简体   繁体   中英

How to parse a nested json object using Get request in retrofit?

i want to make a simple app similar to Uber eats where you can order food and i would like to use the the json object below to get the data and bind it to my recyclerview using retrofit's get method. this is the link for the raw json file: https://pastebin.com/raw/xGGjGVD9

{
"categories": {
  "01": {
    "image": "http://web.archive.org/web/20160530195530if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-fingerfoods2.jpg",
    "name": "Finger Foods"
},
  "02": {
    "image": "http://web.archive.org/web/20160530200622if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-westernsoup1.jpg",
    "name": "Western Soup"
},
  "03": {
    "image": "http://web.archive.org/web/20160530163903if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-easternsoup.jpg",
    "name": "Eastern Soup"
},
  "04": {
    "image": "http://web.archive.org/web/20160530143505if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-sandwich.jpg",
    "name": "Sandwich"
},
  "05": {
    "image": "http://web.archive.org/web/20160531085702if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-pizza.jpg",
    "name": "pizza"
},
  "06": {
    "image": "http://web.archive.org/web/20160530202100if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-pasta1.jpg",
    "name": "pasta"
},
  "07": {
    "image": "http://web.archive.org/web/20160530202109if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-chicken.jpg",
    "name": "Chicken"
},
  "08": {
    "image": "http://web.archive.org/web/20160531085955if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-fish.jpg",
    "name": "Fish"
},
  "09": {
    "image": "http://web.archive.org/web/20160531025351if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-vegetarian.jpg",
    "name": "Chinese Vegetarian"
},
  "10": {
    "image": "http://web.archive.org/web/20160530195451if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-delights.jpg",
    "name": "Medifoods Delights"
},
  "11": {
    "image": "http://web.archive.org/web/20160531054218if_/http://medifoods.my/wp-content/uploads/2015/03/cover-menu-snacks.jpg",
    "name": "Snacks"
   }
 }
}

What i have done so far: i have made 3 classes to fetch the data from the first object with the number "01" but that is just one object i have 9 more objects and if i make a new class for each and every object i will have a lot of classes. is there a better way to parse this json object or i have to make a new class for each object.

public class Properties {

@SerializedName("categories")
private Categories categories;

public Properties(Categories categories) {
    this.categories = categories;
}

public Categories getCategories() {
    return categories;
}

second class:

public class Categories {

@SerializedName("01")
private Foods foods;

public Categories(Foods foods) {
    this.foods = foods;
}

public Foods getFoods() {
    return foods;
}

third class:

public class Foods {

private String image;
private String name;

public Foods(String image, String name) {
    this.image = image;
    this.name = name;
}

public String getImage() {
    return image;
}

public String getName() {
    return name;
}

MainActivivy:

private void getFoodProperties() {
    Call<Properties> getFood = foodAPI.getProperties();
    getFood.enqueue(new Callback<Properties>() {
        @Override
        public void onResponse(Call<Properties> call, Response<Properties> response) {
            if (!(response.isSuccessful())) {
                Log.d(TAG, "onResponse: "+ response.code());
                return;
            }

            Log.d(TAG, "onResponse: " + response.body());
            foods.add(response.body());

            String name = foods.get(0).getCategories().getFoods().getName();
            String image = foods.get(0).getCategories().getFoods().getImage();
            Log.d(TAG, "onResponse: " + name + "  " + image);

        }

Api interface:

public interface FoodApi {

@GET("xGGjGVD9")
Call<Properties> getProperties();

}

I think categories object is a list of food in the json, try to change your categories class.

public class Categories {

    @SerializedName("01")
    private List<Foods> foods;

    public Categories(Foods foods) {
        this.foods = foods;
    }

    public List<Foods> getFoods() {
        return foods;
    }
}

Since you have dynamic keys in the list inside the JSON response you'll need to use a Map for storing the categories.

public class Properties {

    @SerializedName("categories")
    @Expose
    private Map<String, Foods> categories;

    public Map<String, Foods> getCategories() {
        return categories;
    }

    public void setCategories(Map<String, Foods> categories) {
        this.categories = categories;
    }
}

You can iterate through the categories map which will have keys as "01", "02", "03" etc and value as the Foods object. If you need a List from the map you can do the following

List<Foods> list = new ArrayList<Foods>(categories.values());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM