Reputation:
I have some JSON below. Before I only had the items
element in the JSON response and was using retrofit to access this with the following code. 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.
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
// 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);
Upvotes: 0
Views: 3041
Reputation: 5542
{
"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 :
public class DataItem{
private String description [];
private String image1 ;
private String item_id ;
private String title ;
private String recommendation_id ;
}
Upvotes: 2
Reputation: 12541
I would recommend converting your JSON root from array to object:
{
"recommendation": [...],
"items": [...]
}
Then have Retrofit parsed JSON response into a new model, let's say Response
:
public class Response {
private Recommendation[] recommendation;
private Item[] items;
// your getters
}
Upvotes: 0