博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Fastjson 序列化与反序列化
阅读量:6814 次
发布时间:2019-06-26

本文共 4080 字,大约阅读时间需要 13 分钟。

 

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 parseObject(String jsonStr, Class
clazz, Feature... features); // 将JSON字符串反序列化为JavaBean public static
T parseObject(byte[] jsonBytes, // UTF-8格式的JSON字符串 Class
clazz, Feature... features); // 将JSON字符串反序列化为泛型类型的JavaBean public static
T parseObject(String text, TypeReference
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 TreeJSONObject 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 POJOJsonRootBean 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
>() {}.getType();// List
list = JSON.parseObject(dogArr.toString(), type);List
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 stringString 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 bytesbyte[] modelBytes = JSON.toJSONBytes(model);System.out.println("modelBytes:" + modelBytes.length);//117

 

参考文章:

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

转载于:https://www.cnblogs.com/ooo0/p/10902202.html

你可能感兴趣的文章
软件定时器算法
查看>>
pt-archiver 数据删除、迁移工具使用
查看>>
下载网站地址
查看>>
桌面虚拟化浅谈
查看>>
我的友情链接
查看>>
将 TensorFlow 移植到 Android手机,实现物体识别、行人检测和图像风格迁移详细教程...
查看>>
Hyper-V 自动化支持技术
查看>>
VS2010启动调试时报“未能将脚本调试器附加到计算机”
查看>>
Python中的一些面试题(2)
查看>>
无法启动 DTC 分布式事务服务,MS DTC 发生服务特定错误: 3221229584
查看>>
基于HTTP协议的轻量级开源简单队列服务:HTTPSQS
查看>>
【精品教程】Android高手进阶教程pdf分享
查看>>
VB.NET 自动打包程序
查看>>
CISCO引擎RPR SSO
查看>>
LINUX APACHE 安装测试
查看>>
Java导致登录UCS Manager异常
查看>>
HTTP协议
查看>>
Win10怎么改Host文件?Win10编辑host文件方法(无视权限)
查看>>
sql convert and cast
查看>>
我的NodeJS一年之旅总结
查看>>