在 Json 响应中改造访问多个模型
Retrofit accessing multiple models in Json response
我下面有一些JSON。之前我在 JSON 响应中只有 items
元素,并且使用改造来使用以下代码访问它。效果很好。
但现在我已经添加了推荐部分,我已经创建了推荐模型,但是我现在如何从响应的 recommendation/items 部分访问数据?
编辑
也许有更好的方法来返回这些数据。可能有 500 个项目,但只有 20/30 个列表,所以我认为最好拆分而不是复制数据。
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
}
]
}
]
推荐型号
public class Recommendation {
private Boolean following;
private int listId;
private String listName;
public Boolean getFollowing() {
return following;
}
// more getters and setters
}
物品型号
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
}
改造 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);
我建议将您的 JSON 根从数组转换为对象:
{
"recommendation": [...],
"items": [...]
}
然后让 Retrofit 将 JSON 响应解析为一个新模型,假设 Response
:
public class Response {
private Recommendation[] recommendation;
private Item[] items;
// your getters
}
{
"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
}
]
}
型号:
public class Model{
ArrayList<Data> recommendation ;
ArrayList<DataItem> items ;
}
数据模型:
public class Data{
private boolean following ;
private String list_id ;
private String list_name ;
}
数据项模型:
public class DataItem{
private String description [];
private String image1 ;
private String item_id ;
private String title ;
private String recommendation_id ;
}
我下面有一些JSON。之前我在 JSON 响应中只有 items
元素,并且使用改造来使用以下代码访问它。效果很好。
但现在我已经添加了推荐部分,我已经创建了推荐模型,但是我现在如何从响应的 recommendation/items 部分访问数据?
编辑 也许有更好的方法来返回这些数据。可能有 500 个项目,但只有 20/30 个列表,所以我认为最好拆分而不是复制数据。
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
}
]
}
]
推荐型号
public class Recommendation {
private Boolean following;
private int listId;
private String listName;
public Boolean getFollowing() {
return following;
}
// more getters and setters
}
物品型号
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
}
改造 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);
我建议将您的 JSON 根从数组转换为对象:
{
"recommendation": [...],
"items": [...]
}
然后让 Retrofit 将 JSON 响应解析为一个新模型,假设 Response
:
public class Response {
private Recommendation[] recommendation;
private Item[] items;
// your getters
}
{
"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
}
]
}
型号:
public class Model{
ArrayList<Data> recommendation ;
ArrayList<DataItem> items ;
}
数据模型:
public class Data{
private boolean following ;
private String list_id ;
private String list_name ;
}
数据项模型:
public class DataItem{
private String description [];
private String image1 ;
private String item_id ;
private String title ;
private String recommendation_id ;
}