安卓:大解析JSON文件 [英] Android: Parsing large JSON file
问题描述
我创建一个Android应用程序,它应该从一个文件或URL到jsonarray和jsonobjects分析JSON。 问题是,我的JSON是3.3 MB,当我用一个简单的code,是这样的:(不能存取权限我真正的code现在因为IM在工作中,从复制一些教程code;所以可能有一些错误吧)
I'm creating an android application which should parse a Json from a file or url to a jsonarray and jsonobjects. The problem is that my json is 3.3 mb and when i use a simple code, something like this: (can't acces my real code now because im at work, copied some code from tutorial; so there might be some errors in it)
(假设我已经有我的InputStream的内容)
(assuming i already have my inputstream content)
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
String twitterfeed = builder.toString();
}
JSONArray jsonArray = new JSONArray(twittefeed);
Log.i(ParseJSON.class.getName(),
"Number of entries " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));
当我在我的Android设备上运行该code,我解析字符串到jsonArray时遇到内存不足的错误。 我登录了一些东西,我发现,我的总的字符串是17 MB,当我用一个小的JSON文件,如通过twitterfeed左右,code正常工作(3.3 MB JSON文件?!)的。 当我得到这个17 MB串在我的记忆,我不能解析JSON,因为那时我耗尽内存。
When i run this code on my android device, i get an OutOfMemory error when parsing the string to the jsonArray. I logged some things i found that my total string is 17 mb (of a 3.3 mb json file?!) When i use a small json file, like a twitterfeed or so, the code works fine. When i got this 17 mb string in my memory i can't parse the json, because then i run out of memory.
在进行大量的研究,我发现,杰克逊也许是我的解决办法,因为我知道,这是可能的解析一个InputStream。这应该帮助的,因为比我不需要在我的记忆中17 MB串;这是不是最有效的方式,我客串....但我不能得到这真的将工作说清楚,并没有得到它运行自己。 有谁知道这是真的会工作,在那里我可以找到一个教程?
After a lot of research i found that jackson might be my solution, because i understood that it is possible to parse an inputstream. This should help, because than i don't need the 17 mb string in my memory; and this is not the most efficient way i gues.... But i can't get it clear of this really will work, and didn't get it running myself. Does anyone know of this is really will work, and where i can find a tutorial?
我找到了<一href="http://fasterxml.github.com/jackson-core/javadoc/2.0.0/index.html?com/fasterxml/jackson/core/JsonFactory.html"相对=nofollow>createJsonParser - 公共JsonParser createJsonParser(InputStream in)使用并认为这是我的路走...但我不知道如何在我的code实现这一点,并不能找到一个例子。有谁知道这是如何工作?
I found the "createJsonParser -- public JsonParser createJsonParser(InputStream in)" and think this is my way to go... but i don't know how to implement this in my code, and can't find an example. Does anyone know how this works?
推荐答案
您应该使用JSON无论是与GSON或杰克逊流。杰克逊可以使用一种混合的方法为好。这将减少你的内存消耗显著为JSON的只是部分被解析加载到内存中。
You should use json streaming either with gson or jackson. With Jackson you can use a hybrid approach as well. This would reduce your memory consumption significantly as only the portion of json being parsed is loaded into memory.
<一个href="https://sites.google.com/site/gson/gson-user-guide">https://sites.google.com/site/gson/gson-user-guide HTTP://jackson.$c$chaus.org/
https://sites.google.com/site/gson/gson-user-guide http://jackson.codehaus.org/
杰克逊例如<一href="http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/">http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/
这篇关于安卓:大解析JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!