我正在尝试确定如何解析嵌套的JSON数组。此外,数组项有两种不同的类型(类别和项目)。该类别表明有孩子,每个孩子可以是一个类别或项目。我找到了另一个question,如果有人确认,可能会触及我想要做的事情。
{
"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
}]
}
}
为了处理API调用和模型,我使用了GSON。
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
我也使用Retrofit
retrofit = new Retrofit.Builder()
.baseUrl("my url")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();