JSON parsing of FastJson

1_FastJson framework technology
1) Features: Fastjson is a well-written in the Java language features high performance JSON library. It uses a "rapid and orderly assumed to match" algorithm, the JSON Parse performance boost to the extreme, is the fastest JSON Java language library.


2) Download: https://github.com/alibaba/fastjson/wiki

1.1_ the json {} string format into Java objects

1) used in API <T> T parseObject (String json, Class <T> classOfT); // use json object into a Java object Note: The name of the key requirements json java object corresponding to the object class to the same property name

2) using procedure

(1) into a jar fastjson

(2) JSON call the parseObject () method, obtaining the converted object e.g. Java: ShopInfo shopInfo = JSON.parseObject (JSON, ShopInfo.class);

. 3) Test Data 

{
    "id":2,
    "name":"大虾",
    "price":12.3,
    "imagePath":"http://192.168.10.165:8080/L05_Server/images/f1.jpg"
}

4) Examples

  // (1) {} string json converted format into Java objects 
    Private  void jsonToJavaObjectByFastJson () { 

        // a data acquiring or creating JSON 
        String json = "{\ n" + "\ t \" id \ ": 2 , \ "name \": \ " prawns \", \ the n-" 
                +" \ t \ ". price \": 12.3, \ the n-"+" \ t \ "imagePath \": \ "HTTP: //192.168. 10.165: 8080 / L05_Server / Images / f1.jpg \ "\ n-" + "} \ n-" ; 

        // 2 JSON parsing data 
        ShopInfo shopInfo JSON.parseObject = (. JSON, ShopInfo class ); 

        // . 3 display data      
        tv_fastjson_orignal. the setText (JSON); 
        tv_fastjson_last.setText (shopInfo.toString ()); 
    }

 

The format string json 1.2_ [] to Java objects List
. 1) used in API List <T> parseArray (Stringjson arrays into the Java object list note : key name as java object claims json object attribute names corresponding to the same class

2) using procedure

(1) into a jar fastjson

(2) JSON calls parseArray () method, after obtaining the conversion Java collections

   For example: List <ShopInfo> shopInfos = JSON.parseArray (JSON, ShopInfo.class);

. 3) Test Data 

[
    {
        "id":1,
        "imagePath":"http://192.168.10.165:8080/f1.jpg",
        "name":"大虾1",
        "price":12.3
    },
    {
        "id":2,
        "imagePath":"http://192.168.10.165:8080/f2.jpg",
        "name":"大虾2",
        "price":12.5
    }
]

4) Examples

  @ (2) the format json string [] to a Java object List 
    Private  void jsonToJavaListByFastJson () { 

        // . 1 data acquiring or creating JSON 
        String json = "[\ n" + "{\ n" + "\ "the above mentioned id \": 1, \ the n-"+" \ "imagePath \": \ "HTTP: //192.168.10.165: 8080 / f1.jpg \", \ the n-" 
                +" \ "name \": \ "big shrimp. 1 \ ", \ n-" + "\". price \ ": 12.3 \ n-" + "}, \ n-" + "{\ n-" + "\" ID \ ": 2, \ n-" 
                + "\" the imagePath \ ": \" HTTP: //192.168.10.165: 8080 / f2.jpg \ ", \ n-" 
                + "\" name \ ": \" prawn 2 \ ", \ n" + "\" price \ ": 12.5 \ n" + " } \ n" + "]"; 

        // 2 JSON data parsing 
        List <ShopInfo> shopInfos JSON.parseArray = (. JSON, ShopInfo class ); 

        // . 3 display data     
        tv_fastjson_orignal.setText(json);
        tv_fastjson_last.setText(shopInfos.toString());
    }

 

1.3_ converts Java objects json string {}
1) used in the toJSONString String the API (Object Object);

2) using steps

(1) into a jar fastjson

(2) JSON call the toJSONString () method, obtaining the converted data json example:

  ShopInfo shopInfo = new ShopInfo(1, "鲍鱼", 250.0, "baoyu");

  String json = JSON.toJSONString(shopInfo);

3) Examples

 @ (3) converts Java objects json string} { 
    Private  void javaToJsonObjectByFastJson () { 

        // . 1 acquires a Java object 
        ShopInfo shopInfo = new new ShopInfo (. 1, "abalone", 250.0, "Baoyu" ); 

        // 2 generated JSON data 
        String = JSON JSON.toJSONString (shopInfo); 

        // . 3 data     
        tv_fastjson_orignal.setText (shopInfo.toString ()); 
        tv_fastjson_last.setText (JSON); 
    }

 

1.4_ converts the Java object is a List json string []
1) used in the toJSONString String the API (Object Object);

2) using steps

(1) into a jar fastjson

(2) JSON call the toJSONString () method, obtaining the converted data json example:

 

  List<ShopInfo> shops = new ArrayList<>();

  ShopInfo baoyu = new ShopInfo(1, "鲍鱼", 250.0, "baoyu");

  ShopInfo longxia = new ShopInfo(2, "龙虾", 251.0, "longxia");

  shops.add(baoyu); shops.add(longxia);

  String json = JSON.toJSONString(shops);

3)例子 

 @ (4) converts Java objects to List json string [] 
    Private  void javaToJsonArrayByFastJson () {
         // . 1 acquires a collection of Java 
        List <ShopInfo> Shops = new new the ArrayList <> (); 
        ShopInfo Baoyu = new new ShopInfo (. 1, "abalone", 250.0, "Baoyu" ); 
        ShopInfo longxia = new new ShopInfo (2, "lobster", 251.0, "longxia" ); 
        shops.add (Baoyu); 
        shops.add (longxia); 

        // 2 JSON data generated 
        JSON = String JSON.toJSONString (Shops); 

        // . 3 data      
         tv_fastjson_orignal.setText (shops.toString());
        tv_fastjson_last.setText (json);
    }

 

Guess you like

Origin www.cnblogs.com/yanglanwan/p/11295677.html