简体   繁体   English

如何在流模式下读取非常大的JSON文件?

[英]How to read very big JSON files in stream mode?

While working on an API implemented in Java and one of the operations requires to open a big JSON file and returns an object identified by a given string. 在使用Java实现的API上工作时,其中一项操作需要打开一个较大的JSON文件并返回由给定字符串标识的对象。 The file in question is formed by an array of objects, tons of object, and it has no sense to read the whole file and create tons of Java objects into memory only to return one. 有问题的文件由一组对象,大量对象组成,没有意义读取整个文件并将大量Java对象创建到内存中仅返回一个对象。

So, What is a good way to read the JSON file in stream mode? 那么,什么是在流模式下读取JSON文件的好方法?

One of excellent libraries for parsing large JSON files with minimal resources is the popular GSON library . 流行的GSON库是用于以最少的资源解析大型JSON文件的优秀库之一。 It gets at the same effect of parsing the file as both stream and object. 它具有与解析流和对象一样的解析文件的效果。 It handles each record as it passes, then discards the stream, keeping memory usage low. 它处理每条通过的记录,然后丢弃该流,从而保持较低的内存使用率。

Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types) 支持任意复杂的对象(具有深层次的继承层次结构和泛型类型的广泛使用)

Look at this Detailed Tutorial for GSON approach,to solve it problem. 查看此GSON方法的详细教程 ,以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM