需要解析一个大的json文件

时间:2015-11-07 04:32:26

标签: java json

目前我正在进入" java.lang.OutOfMemoryError:Java堆空间" 下面是编写的代码,这里我传递输入流来获得  JsonReader对象继续进行。请让我知道应该是什么  最好的方法。在此先感谢。

JsonReader reader = new JsonReader(new InputStreamReader(inputStream));
    JsonObject element = new JsonParser().parse(reader).getAsJsonObject();

//堆栈跟踪

com.domo.connector.util.MagicException: Failed parsing JSON source: JsonReader at line 1 column 80720228 to Json
    at com.domo.connector.adaptv.api.Client.get(Client.java:125)
    at com.domo.connector.adaptv.api.Client.get(Client.java:86)
    at com.domo.connector.adaptv.api.AdhocReportClient.getFirstPage(AdhocReportClient.java:24)
    at com.domo.connector.adaptv.ProcessRecords.getClientWithFirstPageOfData(ProcessRecords.java:116)
    at com.domo.connector.adaptv.ProcessRecords.readData(ProcessRecords.java:58)
    at com.domo.connector.adaptv.ProcessRecordsFunctionalTest.testCustomer(ProcessRecordsFunctionalTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: com.google.gson.JsonParseException: Failed parsing JSON source: JsonReader at line 1 column 80720228 to Json
    at com.google.gson.JsonParser.parse(JsonParser.java:88)
    at com.domo.connector.adaptv.parser.AdapTVParser.parse(AdapTVParser.java:45)
    at com.domo.connector.adaptv.api.Client.get(Client.java:102)
    ... 29 more
Caused by: java.lang.OutOfMemoryError: Java heap space
    at java.util.ArrayList.<init>(ArrayList.java:132)
    at java.util.ArrayList.<init>(ArrayList.java:139)
    at com.google.gson.JsonArray.<init>(JsonArray.java:40)
    at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:656)
    at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:667)
    at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:659)
    at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:667)
    at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:642)
    at com.google.gson.internal.Streams.parse(Streams.java:44)
    at com.google.gson.JsonParser.parse(JsonParser.java:84)
    at com.domo.connector.adaptv.parser.AdapTVParser.parse(AdapTVParser.java:45)
    at com.domo.connector.adaptv.api.Client.get(Client.java:102)
    at com.domo.connector.adaptv.api.Client.get(Client.java:86)
    at com.domo.connector.adaptv.api.AdhocReportClient.getFirstPage(AdhocReportClient.java:24)
    at com.domo.connector.adaptv.ProcessRecords.getClientWithFirstPageOfData(ProcessRecords.java:116)
    at com.domo.connector.adaptv.ProcessRecords.readData(ProcessRecords.java:58)
    at com.domo.connector.adaptv.ProcessRecordsFunctionalTest.testCustomer(ProcessRecordsFunctionalTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)

1 个答案:

答案 0 :(得分:0)

如果您的文档非常大,则无法在内存中构建DOM树/ JSON对象。您必须使用pull或SAX解析器并逐步计算文档。这将迫使您重写处理JSON对象的代码。

请参阅JSON simpleJackson

如果要解析某种流,基于事件的方法会更好,如果它只是一个文件,拉解析器就足够了。