简体   繁体   English

改造访问Json响应中的多个模型

[英]Retrofit accessing multiple models in Json response

I have some JSON below. 我在下面有一些JSON。 Before I only had the items element in the JSON response and was using retrofit to access this with the following code. 在我只有JSON响应中的items元素并且正在使用改造通过以下代码访问此元素之前。 Which worked fine. 哪个工作正常。

But now I have added the recommendation section, I have created the recommendation model but how do I now access the data from the recommendation/items section of the response? 但是,现在我添加了推荐部分,创建了推荐模型,但是现在如何从响应的推荐/项目部分访问数据?

edit Maybe there is a better way to have this data returned. 编辑也许有更好的方法来返回此数据。 There could be maybe 500 items but only 20/30 lists so I thought it was better to split up instead of duplicating the data. 可能有500个项目,但列表只有20/30,所以我认为最好拆分而不是重复数据。

JSON JSON格式

[
{
    "recommendation": [
        {
            "following": true,
            "list_id": "29",
            "list_name": "list29",
        },
        {
            "following": false,
            "list_id": "28",
            "list_name": "list28",
        }
    ]
},
{
    "items": [
        {
            "description": [
                "line1",
                "line2",
                "line3"
            ],
            "image1": "4367218/img1.jpg",
            "item_id": 5600,
            "title": "Title 1",
            "recommendation_id": 29
        },
        {
            "description": [
                "line1",
                "line2",
                "line3"
            ],
            "image1": "342345/img1.jpg",
            "item_id": 3600,
            "title": "Tite2",
            "recommendation_id": 28
        }
    ]
}
]

Recommendation Model 推荐模型

public class Recommendation {

    private Boolean following;
    private int listId;
    private String listName;

    public Boolean getFollowing() {
        return following;
    }

    // more getters and setters
}

Item Model 项目型号

public class Item {

    private int item_id;
    private String title;
    private ArrayList<String> description;
    private String image1;
    private int recommendation_id;

    // more getters and setters
}

Retrofit GET 改装GET

// feed
@GET("/items/{user_id}/{num_items}")
public void getFeedData(@Path("user_id") int user_id, @Path("num_items") Integer num_items, Callback<List<Item>> response);
{
    "recommendation": [
        "data":{
            "following": true,
            "list_id": "29",
            "list_name": "list29"
        },
       "data": {
            "following": false,
            "list_id": "28",
            "list_name": "list28"
        }
    ]
},
{
    "items": [
        "data_item":{
            "description": [
                "line1",
                "line2",
                "line3"
            ],
            "image1": "4367218/img1.jpg",
            "item_id": 5600,
            "title": "Title 1",
            "recommendation_id": 29
        },
         "data_item":{
            "description": [
                "line1",
                "line2",
                "line3"
            ],
            "image1": "342345/img1.jpg",
            "item_id": 3600,
            "title": "Tite2",
            "recommendation_id": 28
        }
    ]
}

Model: 模型:

public class Model{

ArrayList<Data> recommendation ; 
ArrayList<DataItem> items ;
}

Data Model : 资料模型:

public class Data{

   private boolean following ;
   private String list_id ;
   private String list_name ;

}

DataItem Model : DataItem模型:

public class DataItem{

private String description [];
private String image1 ;
private String item_id ;
private String title ;
private String recommendation_id ;

}

I would recommend converting your JSON root from array to object: 我建议将JSON根从数组转换为对象:

{
    "recommendation": [...],
    "items": [...]
}

Then have Retrofit parsed JSON response into a new model, let's say Response : 然后让Retrofit将JSON响应解析为一个新模型,假设Response

public class Response {
    private Recommendation[] recommendation;
    private Item[] items;
    // your getters
}

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

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