您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何用fastjson處理超大對象和超大JSON文本”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何用fastjson處理超大對象和超大JSON文本”吧!
來看一下示例代碼:
示例對象:
package json.fastjson.StreamApi;import java.util.HashMap;import java.util.Map;public class VO {private int id;private Map<String, Object> attributes = new HashMap<String, Object>();public VO(int id) {super();this.id = id; }public int getId() {return id; }public void setId(int id) {this.id = id; }public Map<String, Object> 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文本這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。