2013-01-19 58 views
2

我在解析与GSON和杰克逊的Android大约11MB的JSON大问题。问题是发生内存不足错误异常并且堆大小不足以完成此过程。 这是我的纸模型类解析在Android 11MB的大JSON

public class Paper { 

public int primaryKey; 

public String title; 

public int entry; 

public Boolean favourite; 

public String comment; 

public int opt; 

public int score; 
} 

这是我的响应模型类

public class Response { 

public List<Paper> papers; 

} 

这是我的JSON字符串

{"Paper":[[{"abstract":"Not Available","title":"A Fully Intraocular 0.0169mm<sup>2<\/sup>/pixel 512-Channel Self-Calibrating Epiretinal Prosthesis in 65nm CMOS","primaryKey":3,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"A Scalable 2.9mW 1Mb/s eTextiles Body Area Network Transceiver with Remotely Powered Sensors and Bi-Directional Data Communication","primaryKey":14,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"A 0.18µm CMOS SoC for a 100m-Range 10fps 200×96-Pixel Time-of-Flight Depth Sensor","primaryKey":20,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"A 12b 1.6GS/s 40mW DAC in 40nm CMOS with >70dB SFDR over Entire Nyquist Bandwidth","primaryKey":26,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"All-Digital Hybrid Temperature Sensor Network for Dense Thermal Monitoring","primaryKey":49,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"32Gb/s Data-Interpolator Receiver with 2-Tap DFE in 28nm CMOS","primaryKey":51,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"A 93% Efficiency Reconfigurable Switched-Capacitor DC-DC Converter Using On-Chip Ferroelectric Capacitors","primaryKey":60,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1},{"abstract":"Not Available","title":"A 45nm CMOS Near-Field Communication Radio with 0.15A/m RX Sensitivity and 4mA Current Consumption in Card Emulation Mode","primaryKey":61,"entry":9,"score":-1,"comment":null,"favourite":false,"opt":1}]]} 

我不知道我做了一个错误。我对文件的含义不了了之。

+0

您应该发布您用于反序列化的代码 – eugen

+0

它充满了不同的对象,所以我不能将它传递到单个类上。 http://206.81.33.5:8080/Conf411/Conf411Service?command=get&id=ISMRM2012 编码容量非常大。请通过上面的链接,并为任何一个特定的类提供一些想法。 – Praveen

+0

可能重复[将大型JSON(InputStream)放入字符串时出现内存不足错误](http://stackoverflow.com/questions/5842201/out-of-memory-error-when-putting-large-json-inputstream-字符串) – rds

回答

4

在完整输入读取之前,使用流解析器并尝试在读取内容时处理内容。这样可以避免在内存中保存完整的结构。

例如,如果您的输入JSON是一个巨大的数组,您可以按元素处理输入元素。

+0

它充满了不同的对象,所以我不能将它传递给单个类。 [JSON URL](http://206.81.33.5:8080/Conf411/Conf411Service?command=get&id=ISMRM2012)编码容量非常大。请通过上面的链接,并为任何一个特定的类提供一些想法。 – Praveen

+0

查看Gson流媒体文档,其中介绍了如何混合流媒体和数据绑定:https://sites.google。com/site/gson/streaming –

0

你也可以试试Genson,检查数据绑定API(new Genson().deserialize(json, ToClass.class))或直接使用流api。

这取决于你想要做什么。 如果你需要内存中的所有数据,那么没有太多的选择,你需要增加它。 如果你可以在读取它时处理内容,并且不需要将它永久地存储在内存中,那么对于流式API(非常高性能且使用极少的内存)它就可以正常工作。

编辑 解决方案

  1. 你想要让你拥有更小的工作要做使用数据绑定。首先看看这个项目https://github.com/joelittlejohn/jsonschema2pojo它允许你基于json例子生成java bean类。如果您正在执行多个ser/deser,那么当您有生成的类时只需执行new Genson().deserialize(json, MyGeneratedClass.class);,则应该重用Genson实例以获得更好的性能。

  2. 如果您不想使用1),如果结构在运行时更改(这会阻止生成类),或者仍然存在内存问题,请使用Gensons streaming api。 它是高效,高性能和易于使用的内存。

+0

它充满了不同的对象,所以我不能将它传递给单个类。 [JSON URL](http://206.81.33.5:8080/Conf411/Conf411Service?command=get&id=ISMRM2012)编码容量非常大。请通过上面的链接,并为任何一个特定的类提供一些想法。 – Praveen

+0

@Praveen我已经用一些更多的细节更新了我的答案。如果它仍然不够,我会尽力为你提供一些例子,但如果你发布你的代码会有帮助。 – eugen

+0

我能够使用你在java中讲述的东西。但在android中不可能。因为android具有很大的堆大小。 – Praveen