android转移json字符串处理,在android中解析JSON字符串的更快捷的方法

该博客讨论了在Android应用程序中解析JSON字符串的传统方法,指出其速度较慢,并提出了可能的改进方案。作者提到尝试使用GSON库来替代当前的解析方法,以期提高性能。同时,代码示例展示了如何遍历并存储JSON数据到Category对象中,最后按字母顺序排序并发送广播通知。
摘要由CSDN通过智能技术生成

我正在使用这种方法来解析JSON字符串,但它太慢了...有没有更好的方法来做到这一点? 谢谢在android中解析JSON字符串的更快捷的方法

synchronized private void parseCategories(String response){

try{

JSONArray categoriesJSONArray = new JSONArray (response);

// looping through All Contacts

for(int i = 0; i < categoriesJSONArray.length(); i++){

JSONObject currentCategory = categoriesJSONArray.getJSONObject(i);

String label="";

String categoryId="";

// Storing each json item in variable

if(currentCategory.has("label"))

label = currentCategory.getString("label");

if(currentCategory.has("id"))

categoryId = currentCategory.getString("id");

if(

label!=null &&

categoryId!=null

)

{

Category toAdd = new Category(categoryId, label);

categories.add(toAdd);

}

}

//Alphabetic order

Collections.sort(

categories,

new Comparator() {

public int compare(Feed lhs, Feed rhs) {

return lhs.getTitle().compareTo(rhs.getTitle());

}

}

);

Intent intent = new Intent("CategoriesLoaded");

LocalBroadcastManager.getInstance(mAppContext).sendBroadcast(intent);

}catch (JSONException e) {

e.printStackTrace();

}

}

2014-09-13

Usi Usi

+0

您是否尝试过使用GSON?我不确定它是否更快,但更方便 –

2014-09-13 10:53:07

+0

您正在设置label =“”和categoryId =“”,因此它们无法为空。所以你不最终添加了很多带有“”作为标签和ID的类别吗? –

2014-09-13 10:57:41

+0

尽管答案都不是最快的。 –

2014-09-13 12:44:26

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值