0

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

}
3
  • Have you tried using GSON? I am not sure if it is faster but it much more convinient Commented Sep 13, 2014 at 10:53
  • You are setting label="" and categoryId="" so there is no way they can be null. So don't you end up adding a lot of categories with "" as label and id? Commented Sep 13, 2014 at 10:57
  • None of the answer is fastest though. Commented Sep 13, 2014 at 12:44

2 Answers 2

1

Here's try following code to start with. You would need Gson library for it.

Gson gson=new Gson();
MyBean myBean=gson.fromJson(response);

Note: Here MyBean class contains the fields present in you json string for e.g. id, along with getter and setters. Rest of all is handled by Gson.

Here's a quick demo.

import com.google.gson.annotations.SerializedName;

public class Box {

  @SerializedName("id")
  private String categoryId;

  // getter and setter
}

Say you JSON looks as following:

{"id":"A12"}

You can parse it as follows:

class Parse{
 public void parseJson(String response){
  Gson gson=new Gson();
  Box box=gson.fromJson(response,Box.class);
  System.out.println(box.getCategoryId());
 }
}

Output :

A12

For more on Gson visit here

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but how Can I do if the parameter "id" in the json string must inserted in the String "id_cat" of the class... is there a way to do that?
Use annotations for them. You can specify json key in class's field annotation.
Thanks, It help. But I have a class called Feed and 2 others that inherits from it... Is there a way to create different annotation for the subclasses?
Yes you can do that (provide different annotations)
Thanks, I can't find an example of different annotations. Can you post an example? Feed has got the ID tag Subscriptions ID tag must be for example id_sub Categories ID tag must be for example id_cat
|
1

Use GSON library. You can convert your object to json string like the following example:

MyClass MyObject;
Gson gson = new Gson();
String strJson = gson.toJson(MyObject);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.