0

Response from URL

[{
  "name": "Bread Bakery & Dairy Product",
  "url": "/images/bread.jpg",
  "id": "5"
}, {
  "name": "Baby Care",
  "url": "/images/baby.jpg",
  "id": "2"
}, {
  "name": "Beverages",
  "url": "/beverages.jpg",
  "id": "6"
}, {
  "name": "Breakfast",
  "url": "/breakfast.jpg",
  "id": "1"
}, {
  "name": "Cleaning",
  "url": "/cleaning.jpg",
  "id": "3"
}, {
  "name": "Kitchen",
  "url": "/images/kitchen.jpg",
  "id": "7"
}, {
  "name": "Others",
  "url": "/images/others.jpg",
  "id": "9"
}, {
  "name": "Personal Care",
  "url": "/images/personal_care.jpg",
  "id": "8"
}, {
  "name": "Snacks",
  "url": "/images/snacks.jpg",
  "id": "4"

}]

SuperCategory.java

public class SuperCategory {

    private int id;
    private String name;
    private String url;

    public SuperCategory() {
    }

    public SuperCategory(int id, String name, String url) {
        this.id = id;
        this.name = name;
        this.url = url;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

and this my logic for JSOn parsing using gson.

List<SuperCategory> superCategories = new ArrayList<>();
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
superCategories = Arrays.asList(mGson.fromJson(response.toString(), SuperCategory[].class));

But it is giving size 0 for superCategories.size().

Can someone help me what is wrong in parsing?

5
  • Try to use superCategories = mGson.fromJson(response.toString(), new TypeToken<List<SuperCategory>>() {}.getType()); Commented May 18, 2016 at 18:29
  • Thanks for response. I tried this. Still same. superCategories.size() is 0 only. Commented May 18, 2016 at 18:32
  • try this tutorial.............. androidhive.info/2016/05/… Commented May 18, 2016 at 18:33
  • what gson version do you use? Commented May 18, 2016 at 18:44
  • I ran your code and was unable to reproduce your error. superCategories.size() is 9. Commented May 18, 2016 at 22:33

1 Answer 1

1

Try changes your code from

GsonBuilder builder = new GsonBuilder(); Gson mGson = builder.create();

become like this

Gson mGson = new Gson();

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

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.