我正在使用这种方法来解析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