简体   繁体   English

如何使用 retrofit 中的 Get 请求解析嵌套的 json object?

[英]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.我想制作一个类似于 Uber eats 的简单应用程序,您可以在其中订购食物,我想使用下面的 json object 来获取数据并使用改造的 get 方法将其绑定到我的 recyclerview。 this is the link for the raw json file: https://pastebin.com/raw/xGGjGVD9这是原始 json 文件的链接: 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. 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会有很多课。 is there a better way to parse this json object or i have to make a new class for each object.有没有更好的方法来解析这个 json object 或者我必须为每个 ZA8CFDE6331BD49EB62AC96F8911 制作一个新的 class

public class Properties {

@SerializedName("categories")
private Categories categories;

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

public Categories getCategories() {
    return categories;
}

second class:第二个 class:

public class Categories {

@SerializedName("01")
private Foods foods;

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

public Foods getFoods() {
    return foods;
}

third class:第三个 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: Api接口:

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.我认为categories object 是 json 中的food列表,尝试更改您的类别 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.由于 JSON 响应内的列表中有动态键,因此您需要使用 Map 来存储类别。

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.您可以遍历类别 map,其键为“01”、“02”、“03”等,值为 Foods object。 If you need a List from the map you can do the following如果您需要 map 的列表,您可以执行以下操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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