Fastjson 序列化与反序列化

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 List对象
Type type = new TypeReference<List<Dog>>() {}.getType();
// List<Dog> list = JSON.parseObject(dogArr.toString(), type);
List<Dog> list = JSON.parseArray(dogArr.toString(), Dog.class);
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值