我有一个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 space
上decompressedStringBuffer.append(part);
。由于文件太大而无法存储在内存中,我考虑将其存储在文件中,然后再读取文件转换为JSON,但使用FileInputStream
创建的JSON对象会给我一个Caused by:java.lang.OutOfMemoryError: Java heap space
。
JSON中唯一有用的数据是name
和children
,在profileData
密钥下。
有没有办法在解析inputStream的时候将它们转换为JSONObject
而忽略其他呢?
如果有更好的方式有人可以想到我也会很感激。
那个文件有多大? – john16384
我会看看Jackson Streaming API,它不会要求您将整个文件放在内存中。 – john16384
未压缩文件大约为130MB。 –