Fastjson 序列化与反序列化

 

JSON这个类是fastjson API的入口,主要的功能都通过这个类提供。

序列化API

    // 将Java对象序列化为JSON字符串,支持各种各种Java基本类型和JavaBean
    public static String toJSONString(Object object, SerializerFeature... features);

    // 将Java对象序列化为JSON字符串,返回JSON字符串的utf-8 bytes
    public static byte[] toJSONBytes(Object object, SerializerFeature... features);

    // 将Java对象序列化为JSON字符串,写入到Writer中
    public static void writeJSONString(Writer writer,
                                       Object object,
                                       SerializerFeature... features);

    // 将Java对象序列化为JSON字符串,按UTF-8编码写入到OutputStream中
    public static final int writeJSONString(OutputStream os,
                                            Object object,
                                            SerializerFeature... features);

JSON字符串反序列化API

    // 将JSON字符串反序列化为JavaBean
    public static <T> T parseObject(String jsonStr, 
                                    Class<T> clazz, 
                                    Feature... features);

    // 将JSON字符串反序列化为JavaBean
    public static <T> T parseObject(byte[] jsonBytes,  // UTF-8格式的JSON字符串
                                    Class<T> clazz, 
                                    Feature... features);

    // 将JSON字符串反序列化为泛型类型的JavaBean
    public static <T> T parseObject(String text, 
                                    TypeReference<T> type, 
                                    Feature... features);

    // 将JSON字符串反序列为JSONObject
    public static JSONObject parseObject(String text);

 

Demo

String jsonStr = "{  " +
                "  \"animals\": {  " +
                "    \"dog\": [{  " +
                "        \"name\": \"Rufus\",  " +
                "        \"breed\": \"labrador\"  " +
                "      },  " +
                "      {  " +
                "        \"name\": \"Marty\",  " +
                "        \"breed\": \"whippet\"  " +
                "      }  " +
                "    ],  " +
                "    \"cat\": {  " +
                "      \"name\": \"Matilda\"  " +
                "    }  " +
                "  }  " +
                "}";

 

parse Tree

//parse Tree
JSONObject jsonObj = JSON.parseObject(jsonStr);
System.out.println("jsonObj:" + jsonObj);//{"animals":{"cat":{"name":"Matilda"},"dog":[{"name":"Rufus","breed":"labrador"},{"name":"Marty","breed":"whippet"}]}}
JSONObject animals = jsonObj.getJSONObject("animals");
System.out.println("animals:" + animals);//{"cat":{"name":"Matilda"},"dog":[{"name":"Rufus","breed":"labrador"},{"name":"Marty",","breed":"whippet"}]}
JSONArray dogArr = animals.getJSONArray("dog");
System.out.println("dogArr:" + dogArr);//[{"name":"Rufus","breed":"labrador"},{"name":"Marty","breed":"whippet"}]
JSONObject dog0 = dogArr.getJSONObject(0);
System.out.println("dog0:" + dog0);//{"name":"Rufus","breed":"labrador"}
String dog0name = dog0.getString("name");
System.out.println("dog0name:" + dog0name);//Rufus
 

parse POJO

//parse POJO
JsonRootBean model = JSON.parseObject(jsonStr, JsonRootBean.class);
System.out.println("model:" + model);//JsonRootBean{animals=Animals{dog=[Dog{name='Rufus', breed='labrador'}, Dog{name='Marty', breed='whippet'}], cat=Cat{name='Matilda'}}}

 

parse POJO Generic

//parse POJO Generic
Type type = new TypeReference<List<Dog>>() {
}.getType();
List<Dog> list = JSON.parseObject(dogArr.toString(), type);
System.out.println("list:" + list);//[Dog{name='Rufus', breed='labrador'}, Dog{name='Marty', breed='whippet'}]

 

convert POJO to json string

//convert POJO to json string
String modelStr = JSON.toJSONString(model);
System.out.println("modelStr:" + modelStr);//{"animals":{"cat":{"name":"Matilda"},"dog":[{"breed":"labrador","name":"Rufus"},{"breed":"whippet","name":"Marty"}]}}

 

convert POJO to json bytes

//convert POJO to json bytes
byte[] modelBytes = JSON.toJSONBytes(model);
System.out.println("modelBytes:" + modelBytes.length);//117

 

参考文章:

https://www.w3cschool.cn/fastjson/fastjson-api.html

上一篇:重写equals方法


下一篇:什么是类?