In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use fastjson to deal with super-large objects and super-large JSON text". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use fastjson to deal with super-large objects and super-large JSON text".
Take a look at the sample code:
Example object:
Package json.fastjson.StreamApi;import java.util.HashMap;import java.util.Map;public class VO {private int id;private Map attributes= 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. Serialization
1.1.The serialization of super large JSON array
If your JSON format is a huge JSON array with many elements, call startArray first, then write objects one by one, and then call endArray.
Test class:
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(); }} 输出结果: 程序运行之后会产生一个文件:Contents of the file:
{"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}}
one
2. Deserialization
2.1.The deserialization of super large JSON array
Test class:
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 {/ / read the output file above 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 ();}}
Output result:
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.The deserialization of very large JSON objects
Test class:
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 {/ / read the output file above 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 ();}}
Output result:
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= {}] Thank you for your reading The above is the content of "how to use fastjson to deal with super-large objects and super-large JSON text". After the study of this article, I believe you have a deeper understanding of how to use fastjson to deal with super-large objects and super-large JSON text. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.