【发布时间】:2016-06-05 14:23:11
【问题描述】:
基本上,我有一个 JSON 字符串,可以在链接 Six Critics 中找到。当我使用 JSON 解析来分隔字符串中的字段时,在 asynctask onPostExecute 中,每次查看时都会刷新片段。但是当我通过获取两个分隔符的索引之间的子字符串来使用字符串扫描方法时,如果之前加载了片段,则不会刷新片段内容。
这种行为的原因是什么? JSON解析是否过于密集?建议这样做是什么?
编辑:
JSONObject jsonWholePage = new JSONObject(result);
JSONArray posts = jsonWholePage.optJSONArray("posts");
for(int i=0; I<posts.length(); i++){
JSONObject postObject = posts.getJSONObject(i);
JSONArray cate = postObject.optArray("categories");
int num_cate = cate.length();
String category = cate.getJSONObject(0).optString("title");
String title = postObject.optString("title");
//do something with title and category
我正在使用默认的 Android JSON 库 org.json.*。
【问题讨论】:
-
你能提供一些示例代码吗?如果你有 json,那么你应该使用提供的工具来解析,而不是 substring
-
您没有为任何人提供足够的信息来帮助您。请看这个:stackoverflow.com/help/how-to-ask
-
那么,让我添加实际代码。
-
不要手动解析 json 字符串,而是尝试使用 google 的 gson java 库,该库可用于将 json 字符串转换为等效的 Java 对象 - github.com/google/gson
-
@Clive Seebregts 我认为您可以将您的评论表达为答案。
标签: java android json android-fragments string-search