什么是处理JSON对象的最佳方式是什么? [英] What is the best way to deal with JSON object?

查看:158
本文介绍了什么是处理JSON对象的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个重新presents JSON提要一个大的字符串。我的应用程序下载该饲料由远程Web服务。

I have a big string that represents JSON feed. My app downloads this feed from remote web service.

问题:

1)当我下载JSON的饲料,我应该在哪里存放呢?现在我将它存储在应用程序preferences并能正常工作。我只是感兴趣,如果有任何理由不这样做,或是否有更好的/更快的选项,例如内部存储或其他什么东西?

1) Once I download JSON feed, where should I store it? Right now I am storing it in app Preferences and it works fine. I am just interested if there is any reason not to do that, or if there are better/faster options, like Internal Storage or something else?

2)当我即将离开这个饲料在我的活动显示的数据,我做这样的事情:

2) When I am about to show data from this feed in my activity, I do something like this:

JSONObject jsonObj = new JSONObject(json_string);
JSONArray jsonArr = jsonObj.getJSONArray("root");
for (int m = 0; m < jsonArr.length(); m++) {
    ....
}

的问题是,解析JSON提要,第一行是排序昂贵。在我目前的规模饲料(可能在未来的发展),它可能需要半秒来完成,虽然不大,但仍然是一个问题。例如,我按一下按钮调用活动(对数据),这1/2秒的停顿是明显的,而不是好。

The problem is, the first line that parses JSON feed is sort of expensive. At my current size feed (that might grow in the future) it takes maybe half a second to complete, not much, but still a problem. For example, I click button to call activity (to show data) and this 1/2 sec pause is noticeable and not good.

要避免这种情况的停顿,我也许应该立即解析JSON下载饲料后。那么问题是,我应该保持已解析的JSON对象?

To avoid this pause, I should probably parse JSON immediately after feed is downloaded. Then question is, where should I keep parsed JSON object?

我)我不能保存JSON对象preferences因为只有原始数据可以存储在那里。

i) I can't save JSON object to preferences since only primitive data can be stored there.

二)我应该做解析每一个我的应用程序启动时,使我经过分析的JSON对象(我有更多的人)全球(应用层),所以我可以从我所有的活动访问它们?

ii) Should I do parsing every time my app starts, and make my parsed JSON objects (I have more of them) global (on application level) so I can access them from all of my activities?

III)或者,我相信我的第三个选择是将申报JSON objests静态的,所以他们可以很容易从其他活动的访问?

iii) Or, I believe my 3rd option would be to declare JSON objests static so they can be easy accessed from other activities?

什么是做到这一点的最好方法是什么?

What is the best way to do this?

THX。

推荐答案

如果速度确实是一个问题,一个简单的方法来显著加快解析(以及写出的)JSON数据是使用的杰克逊解析JSON数据。相比org.json库(Android SDK中附带),它至少是3 - 快5倍,如果你只是想解析到地图或列表上(http://www.cowtown$c$cr.com/blog/archives/ 2009/02 / entry_204.html)。 但是,如果你可以将其绑定到Java对象是尚未速度更快,占用内存更少为好。

If speed is truly a problem, an easy way to significantly speed up parsing (as well as writing out of) JSON data would be to use Jackson for parsing JSON data. Compared to org.json library (which Android SDK ships with) it is at least 3 - 5 times faster if you just want parse to Maps or Lists (http://www.cowtowncoder.com/blog/archives/2009/02/entry_204.html). But if you can bind it to Java objects it is yet faster and uses less memory as well.

如果你想保存JSON数据,它通常只是序列化为文件,或在数据库中的BLOB;但除非你真的要,最好只坚持到Java对象的JSON数据创建的。

If you want to save JSON data it is typically just serialized as files, or as BLOBs in databases; but unless you really have to, it's better to just hold on to Java objects created from JSON data.

这篇关于什么是处理JSON对象的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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