处理JSON对象的最佳方法是什么?

时间:2022-09-11 14:59:01

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

我有一个代表JSON feed的大字符串。我的应用从远程Web服务下载此Feed。

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?

1)一旦我下载JSON feed,我应该在哪里存储它?现在我将它存储在应用程序首选项中,它工作正常。我只是感兴趣,如果有任何理由不这样做,或者是否有更好/更快的选项,如内部存储或其他什么?

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

2)当我要在我的活动中显示来自此Feed的数据时,我会执行以下操作:

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 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 feed的第一行很昂贵。在我目前的大小饲料(可能会在未来增长)完成,可能需要半秒钟,但不是很多,但仍然是一个问题。例如,我点击按钮来调用活动(显示数据),这个1/2秒的暂停是显而易见的,并不好。

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

为了避免这种暂停,我应该在下载feed之后立即解析JSON。然后问题是,我应该在哪里保存解析的JSON对象?

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

i)我无法将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?

ii)我应该在每次启动应用程序时进行解析,并将解析后的JSON对象(我有更多这些对象)全局(在应用程序级别),以便我可以从我的所有活动中访问它们吗?

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

iii)或者,我相信我的第三个选择是将JSON objests声明为静态,以便从其他活动中轻松访问它们?

What is the best way to do this?

做这个的最好方式是什么?

THX.

3 个解决方案

#1


3  

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数据的简单方法就是使用Jackson来解析JSON数据。与org.json库(Android SDK附带)相比,如果您只想解析地图或列表,它至少要快3-5倍(http://www.cowtowncoder.com/blog/archives/2009/02/ entry_204.html)。但是如果你可以将它绑定到Java对象,那么它更快,并且使用更少的内存。

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数据,通常只将其序列化为文件或数据库中的BLOB;但除非你真的需要,否则最好只保留从JSON数据创建的Java对象。

#2


1  

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

你需要存放吗?如果您从网页下载一次,这就是您所需要的,将其存储在共享首选项中就可以了。如果你需要得到它,为什么甚至存储它?您也可以将其存储在缓存目录中

#3


1  

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

听起来你应该使用SQLite数据库来存储从JSON中提取的值。这应该可以帮助您入门: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.

然后,应用程序中的所有活动都可以在数据库中查询数据。如果使用ListView查看信息,则可以使用CursorAdapter轻松填充列表。

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

您甚至可以创建内容提供商以与其他应用程序共享此信息

#1


3  

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数据的简单方法就是使用Jackson来解析JSON数据。与org.json库(Android SDK附带)相比,如果您只想解析地图或列表,它至少要快3-5倍(http://www.cowtowncoder.com/blog/archives/2009/02/ entry_204.html)。但是如果你可以将它绑定到Java对象,那么它更快,并且使用更少的内存。

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数据,通常只将其序列化为文件或数据库中的BLOB;但除非你真的需要,否则最好只保留从JSON数据创建的Java对象。

#2


1  

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

你需要存放吗?如果您从网页下载一次,这就是您所需要的,将其存储在共享首选项中就可以了。如果你需要得到它,为什么甚至存储它?您也可以将其存储在缓存目录中

#3


1  

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

听起来你应该使用SQLite数据库来存储从JSON中提取的值。这应该可以帮助您入门: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.

然后,应用程序中的所有活动都可以在数据库中查询数据。如果使用ListView查看信息,则可以使用CursorAdapter轻松填充列表。

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

您甚至可以创建内容提供商以与其他应用程序共享此信息