Android:解析大型 JSON 文件 [英] Android: Parsing large JSON file

查看:21
本文介绍了Android:解析大型 JSON 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 android 应用程序,该应用程序应该将 Json 从文件或 url 解析为 jsonarray 和 jsonobjects.问题是我的 json 是 3.3 mb,当我使用一个简单的代码时,如下所示:(现在无法访问我的真实代码,因为我在工作,从教程中复制了一些代码;所以其中可能存在一些错误)

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)

(假设我已经有了我的输入流内容)

(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 设备上运行此代码时,在将字符串解析为 jsonArray 时出现 OutOfMemory 错误.我记录了一些我发现我的总字符串是 17 mb(一个 3.3 mb json 文件?!)当我使用一个小的 json 文件时,比如 twitterfeed 左右,代码工作正常.当我在我的记忆中得到这个 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.

经过大量研究,我发现 jackson 可能是我的解决方案,因为我知道可以解析输入流.这应该会有所帮助,因为我不需要记忆中的 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?

我找到了 "createJsonParser -- public JsonParser createJsonParser(InputStream in)" 并认为这是我要走的路......但我不知道如何在我的代码中实现这一点,也找不到例子.有谁知道这是如何工作的?

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 或 jackson 一起使用.对于 Jackson,您也可以使用混合方法.这将显着减少您的内存消耗,因为只有被解析的 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.

https://sites.google.com/site/gson/gson-user-guidehttp://jackson.codehaus.org/

https://sites.google.com/site/gson/gson-user-guide http://jackson.codehaus.org/

jackson 示例 http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/

A jackson example http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/

这篇关于Android:解析大型 JSON 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆