Android : Faster way to parse a JSON String in android

on Saturday, September 13, 2014


I'm using this method to parse a JSON string, but it is too slow... is there a better way to do it? Thanks



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();
}

}

0 comments:

Post a Comment