2017-04-02 46 views
0

我有一个JSON文件是这样的:选择性JSON解析Java中

{"start":1489730400000, 
"end":1489733999999, 
"interval":1000, 
"weight":1, 
"augmented": true, 
"profileName":"Selene/prod", 
"prunedSamples":0, 
"fleet":{"c4.2xlarge":14.313278698132972}, "costPerSecond":0.000008259246540496541, 
"profileData":{ 
    "name":"ALL", 
    "states":{"BLOCKED":2281, "NEW":0, "RUNNABLE":125833, "TERMINATED":0, "TIMED_WAITING":23170429, "WAITING":59901416}, 
    "location": "0", 
    "hidden": [], 
    "children":[{"name":"GarbageCollector.gc", 
    "states":{"BLOCKED":0, "NEW":0, "RUNNABLE":17069}, 
    "location": "0.0", 
    "hidden": [], 
    "children":[{"name":"ConcurrentMarkSweep.gc", 
       "states":{"BLOCKED":0, "NEW":0, "RUNNABLE":14977}, 
       "location": "0.0.0", 
       "hidden": [], 
       "level": 1}, 
       {"name":"ParNew.gc", 
       "states":{"BLOCKED":0, "NEW":0, "RUNNABLE":2092}, 
       "location": "0.0.1", 
       "hidden": [], 
       "level": 1}] 
}} 

这只是其中的一部分。我得到一个GZip格式更大的文件,我首先解压并将解压缩的部分存储在一个字符串中。我使用下面的代码为:

URL url = new URL("http://example.com/Selene%20Prod?start=1490234400000&end=1490237999999&maxDepth=200&minimumCountsThreshold=0.00"); 
URLConnection myUrlConnection = url.openConnection(); 
GZIPInputStream gZIPInputStream = new GZIPInputStream(myUrlConnection.getInputStream()); 
StringBuffer decompressedStringBuffer = new StringBuffer(); 
int bytes_read; 
while ((bytes_read = gZIPInputStream.read(buffer)) > 0) { 
    String part = new String(buffer, 0 ,bytes_read, "UTF-8"); 
    decompressedStringBuffer.append(part); 
} 
gZIPInputStream.close(); 
String decompressedString = decompressedStringBuffer.toString(); 
JSONObject obj = new JSONObject(decompressedString); 
JSONArray profileData = obj.getJSONObject("profileData").getJSONArray("children"); 

我的代码给Caused by: java.lang.OutOfMemoryError: Java heap spacedecompressedStringBuffer.append(part);。由于文件太大而无法存储在内存中,我考虑将其存储在文件中,然后再读取文件转换为JSON,但使用FileInputStream创建的JSON对象会给我一个Caused by:java.lang.OutOfMemoryError: Java heap space

JSON中唯一有用的数据是namechildren,在profileData密钥下。

有没有办法在解析inputStream的时候将它们转换为JSONObject而忽略其他呢?

如果有更好的方式有人可以想到我也会很感激。

+0

那个文件有多大? – john16384

+0

我会看看Jackson Streaming API,它不会要求您将整个文件放在内存中。 – john16384

+0

未压缩文件大约为130MB。 –

回答

0

我认为你不需要去JACKSON库,只是试图找到一种方法来解析你的GSON中的大型JSON。

尝试This resource这是GSON作者的答案。 在同一页面上,您还可以使用流式传输和树型模式(如果您想在杰克逊上移动)找到JACKSON的工作示例

除了提供以下链接以防万一您想增加堆大小: How to increase Java heap size