Fastjson 序列化与反序列化

Posted ooo0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于Fastjson 序列化与反序列化的主要内容,如果未能解决你的问题,请参考以下文章

json大数据量序列化与反序列化慢

Springboot整合FastJson

springboot使用FastJson返回Json视图

c# 多个json字符串反序列化

使用C# json 二维数组 反序列化

一行代码实现iOS序列化与反序列化