如何用fastjson处理超大对象和超大JSON文本
发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,这篇文章主要讲解了"如何用fastjson处理超大对象和超大JSON文本",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"如何用fastjson处理超大对
千家信息网最后更新 2025年02月05日如何用fastjson处理超大对象和超大JSON文本
这篇文章主要讲解了"如何用fastjson处理超大对象和超大JSON文本",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"如何用fastjson处理超大对象和超大JSON文本"吧!
来看一下示例代码:
示例对象:
package json.fastjson.StreamApi;import java.util.HashMap;import java.util.Map;public class VO {private int id;private Mapattributes = new HashMap ();public VO(int id) {super();this.id = id; }public int getId() {return id; }public void setId(int id) {this.id = id; }public Map getAttributes() {return attributes; }@Overridepublic String toString() {return "VO [id=" + id + ", attributes=" + attributes + "]"; }}
一、序列化
1.1、超大JSON数组序列化
如果你的JSON格式是一个巨大的JSON数组,有很多元素,则先调用startArray,然后挨个写入对象,然后调用endArray。
测试类:
package json.fastjson.StreamApi;import java.io.FileWriter;import java.io.IOException;import com.alibaba.fastjson.JSONWriter;public class TestHugeArraySerialize {public static void main(String[] args) throws IOException { JSONWriter writer = new JSONWriter(new FileWriter("hugeArray.json")); writer.startArray();for (int i = 0; i < 10; ++i) { writer.writeValue(new VO(i)); } writer.endArray(); writer.close(); }}
输出结果:
程序运行之后会产生一个文件:
文件内容:
[{"attributes":{},"id":0},{"attributes":{},"id":1},{"attributes":{},"id":2},{"attributes":{},"id":3},{"attributes":{},"id":4},{"attributes":{},"id":5},{"attributes":{},"id":6},{"attributes":{},"id":7},{"attributes":{},"id":8},{"attributes":{},"id":9}]
1
1.2、超大JSON对象序列化
如果你的JSON格式是一个巨大的JSONObject,有很多Key/Value对,则先调用startObject,然后挨个写入Key和Value,然后调用endObject。
测试类:
package json.fastjson.StreamApi;import java.io.FileWriter;import java.io.IOException;import com.alibaba.fastjson.JSONWriter;public class TestHugeObjectSerialize {public static void main(String[] args) throws IOException { JSONWriter writer = new JSONWriter(new FileWriter("hugeObject.json")); writer.startObject();for (int i = 0; i < 10; ++i) { writer.writeKey("x" + i); writer.writeValue(new VO(i)); } writer.endObject(); writer.close(); }}
输出结果:
程序运行之后会产生一个文件:
文件内容:
{"x0":{"attributes":{},"id":0},"x1":{"attributes":{},"id":1},"x2":{"attributes":{},"id":2},"x3":{"attributes":{},"id":3},"x4":{"attributes":{},"id":4},"x5":{"attributes":{},"id":5},"x6":{"attributes":{},"id":6},"x7":{"attributes":{},"id":7},"x8":{"attributes":{},"id":8},"x9":{"attributes":{},"id":9}}
1
二、反序列化
2.1、超大JSON数组反序列化
测试类:
package json.fastjson.StreamApi;import java.io.FileReader;import java.io.IOException;import com.alibaba.fastjson.JSONReader;public class TestHugeArrayDeserialize {public static void main(String[] args) throws IOException {// 读入上面输出的文件JSONReader reader = new JSONReader(new FileReader("hugeArray.json")); reader.startArray();while (reader.hasNext()) { VO vo = reader.readObject(VO.class); System.out.println(vo); } reader.endArray(); reader.close(); }}
输出结果:
VO [id=0, attributes={}]VO [id=1, attributes={}]VO [id=2, attributes={}]VO [id=3, attributes={}]VO [id=4, attributes={}]VO [id=5, attributes={}]VO [id=6, attributes={}]VO [id=7, attributes={}]VO [id=8, attributes={}]VO [id=9, attributes={}]
2.2、超大JSON对象反序列化
测试类:
package json.fastjson.StreamApi;import java.io.FileReader;import java.io.IOException;import com.alibaba.fastjson.JSONReader;public class TestHugeObjectDeserialize {public static void main(String[] args) throws IOException {// 读入上面输出的文件JSONReader reader = new JSONReader(new FileReader("hugeObject.json")); reader.startObject();while (reader.hasNext()) { String key = reader.readString(); VO vo = reader.readObject(VO.class); System.out.println(key + ":" + vo); } reader.endObject(); reader.close(); }}
输出结果:
x0:VO [id=0, attributes={}]x1:VO [id=1, attributes={}]x2:VO [id=2, attributes={}]x3:VO [id=3, attributes={}]x4:VO [id=4, attributes={}]x5:VO [id=5, attributes={}]x6:VO [id=6, attributes={}]x7:VO [id=7, attributes={}]x8:VO [id=8, attributes={}]x9:VO [id=9, attributes={}]
感谢各位的阅读,以上就是"如何用fastjson处理超大对象和超大JSON文本"的内容了,经过本文的学习后,相信大家对如何用fastjson处理超大对象和超大JSON文本这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
对象
序列
文件
输出
文本
处理
内容
结果
测试
数组
学习
巨大
格式
示例
程序
运行
代码
元素
就是
思路
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
上海博文网络技术有限公司
广州胜效网络技术有限公司
湖北三力通信网络技术资质
大安全下的网络安全现状分析
语音情绪数据库
华为研究生网络安全工程师
阿里云怎么管理购买的服务器
c 实时查询数据库
王者抢先服服务器怎么进入
无线传感器网络技术检测
数据库视图总结
数据库提示单元格的值无效
jsp数据库显示
网络安全知识讲座读后感
自动连接服务器
服务器异常怎么回事啊
用手机做打印服务器
蝙蝠软件的服务器是哪里的
借助数据库管理系统和技术
软件开发小标
广州胜效网络技术有限公司
爬虫软件需要服务器吗
魔兽对战平台如何卡断开服务器
信息网络安全宣传海报
电力网络安全措施
2005数据库增加用户
公安筑牢网络安全阵地
地灾的软件开发项目有哪些
中小学网络安全教育问卷
优秀数据库开发工程师学习哪些