我正在使用這種方法來解析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<Feed>() {
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();
}
}
您是否嘗試過使用GSON?我不確定它是否更快,但更方便 – hoomi 2014-09-13 10:53:07
您正在設置label =「」和categoryId =「」,因此它們無法爲空。所以你不最終添加了很多帶有「」作爲標籤和ID的類別嗎? – MTilsted 2014-09-13 10:57:41
儘管答案都不是最快的。 – mmlooloo 2014-09-13 12:44:26