我嘗試使用Java解析大型json文件(更多600Mo)。 我json
文件看起來像:「OutOfMemoryError:超出GC開銷限制」:使用java解析大型json文件
{
"0" : {"link_id": "2381317", "overview": "mjklmklmklmklmk", "founded": "2015", "followers": "42", "type": "Gamer", "website": "http://www.google.com", "name": "troll", "country": "United Kingdom", "sp": "Management Consulting" },
"1" : {"link_id": "2381316", "overview": "mjklmklmklmklmk", "founded": "2015", "followers": "41", "type": "Gamer", "website": "http://www.google2.com", "name": "troll2", "country": "United Kingdom", "sp": "Management Consulting" }
[....]
"345240" : {"link_id": "2381314", "overview": "mjklmklmklmklmk", "founded": "2015", "followers": "23", "type": "Gamer", "website": "http://www.google2.com", "name": "troll2", "country": "United Kingdom", "sp": "Management Consulting" }
}
和我的代碼看起來像這樣:
public class dumpExtractor {
private static final String filePath = "/home/troll/Documents/analyse/lol.json";
public static void main(String[] args) {
try {
// read the json file
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
Iterator<JSONObject> iterator = jsonObject.values().iterator();
while (iterator.hasNext()) {
JSONObject jsonChildObject = iterator.next();
System.out.println("==========================");
String name = (String) jsonChildObject.get("name");
System.out.println("Industry name: " + name);
String type = (String) jsonChildObject.get("type");
if (type != null && !type.isEmpty()) {
System.out.println("type: " + type);
}
String sp = (String) jsonChildObject.get("sp");
if (sp != null && !sp.isEmpty()) {
System.out.println("sp: " + sp);
}
System.out.println("==========================");
}
System.out.println("done ! ");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
我已經得到這個錯誤:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.HashMap.createEntry(HashMap.java:897)
at java.util.HashMap.addEntry(HashMap.java:884)
at java.util.HashMap.put(HashMap.java:505)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
我該如何解決呢?
在此先感謝。
增加的問題是完整的對象過於龐大。嘗試逐行閱讀該文件並分別解析每個嵌套對象。 –
你可以添加你的導入語句的行,以便我們可以看到你使用的Parser? –
@CarlosBribiescas堆棧跟蹤顯示它:'org.json.simple.parser.JSONParser' – Andreas