开发者

What is the best way to deal with JSON object?

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

Questions:

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) 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++) {
    ....
}

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 ta开发者_运维技巧kes 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.

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

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

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) 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.


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.

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.


Do you need to store it? If you download it once from a webpage and that's all you need, storing it in shared preferences is fine. If you need to get it a lot, why even store it? You could also store it in the cache directory


It sounds like you should be using the SQLite database to store the values you pull out from the JSON. This should help you get started: http://developer.android.com/guide/topics/data/data-storage.html#db

All of your activities within your application can then query the database for the data. If you are using a ListView to view the information, you would be able to use a CursorAdapter to easily populate the list.

You could even create a Content Provider to share this information with other applications

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜