I am trying to determine how I can parse a JSON array which is nested. Additionally, the array items are of two different types (category and item). The category states that there are children and each child can be a category or an item. I found another question, which might touch on what I am trying to do, if anyone could confirm.
{
"content": {
"elements":[
{
"type": "category",
"id": "1",
"title": "category 1",
"description": "category description 1",
"image":
{
"src": null,
"thumb": null
},
"elements":[
{
"type": "category",
"id": "1.1",
"title": "category 1.1",
"description": "category description 1.1",
"image":
{
"src": null,
"thumb": null
},
"elements":[
{
"type": "item",
"id": "1.1.1",
"title": "item 1.1.1",
"description": "item description 1.1.1",
"images":[],
"options":[],
"price":0.00,
"vat": 0
}, {
"type": "category",
"id": "1.1.2",
"title": "category 1.1.2",
"description": "category description 1.1.2",
"image":
{
"src": null,
"thumb": null
},
"elements":[
{
"type": "item",
"id": "1.1.2.1",
"title": "item 1.1.2.1",
"description": "item description 1.1.2.1",
"images":[],
"options":[],
"price":0.00,
"vat": 0
}]
}]
},{
"type": "category",
"id": "1.2",
"title": "category 1.2",
"description": "category description 1.2",
"image":
{
"src": null,
"thumb": null
},
"elements": [
{
"type": "item",
"id": "1.2.1",
"title": "item 1.2.1",
"description": "item description 1.2.1",
"images":[],
"options":[],
"price":0.00,
"vat": 0
}]
}]
}, {
"type": "item",
"id": 2,
"title": "item 2",
"description": "item description 2",
"images":[],
"options":[],
"price":0.00,
"vat": 0
}]
}
}
To handle API calls and models, I make use of GSON.
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
I also use Retrofit
retrofit = new Retrofit.Builder()
.baseUrl("my url")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();