我有以下 json 我试图解析但没有成功:
[{
"id":10,
"description":"Prueba",
"name":"Prueba",
"price":"3.5",
"updated_at":"2013-06-10T13:41:25Z",
"images":
[{
"image":{
"id":6,
"type":"Image",
"description":"Prueba",
"name":"Prueba",
"data_updated_at":"2013-06-10T13:41:24Z",
"updated_at":"2013-06-10T13:41:25Z",
"position":1,
"data_file_size":9599,
"data_file_name":"tortitas.jpg",
"image_original":"tortitas.jpg",
"image_medium":"tortitas_medium.jpg",
"image_thumb":"tortitas_thumb.jpg"
}
}]
},
{
"id":11,
"description":"Tortitas ricas ricas",
"name":"Tortitas",
"price":"3.56",
"updated_at":"2013-06-10T14:37:58Z",
"images":
[{
"image":{
"id":7,
"type":"Image",
"description":"Tortitas",
"name":"Tortitas",
"data_updated_at":"2013-06-10T14:37:57Z",
"updated_at":"2013-06-10T14:37:58Z",
"position":1,
"data_file_size":9599,
"data_file_name":"tortitas.jpg",
"image_original":"tortitas.jpg",
"image_medium":"tortitas_medium.jpg",
"image_thumb":"tortitas_thumb.jpg"
}
}]
}
}]
我还需要获取名称、ID、价格和所有图像数据,我已经搜索和谷歌搜索没有退出,这就是我在这里结束的原因,因为通常我在这里找到了我的问题的所有答案,但这次没有。这是我现在用来解析数据的代码,我能够获取名称、ID、价格,但对图像没有运气。
JSONArray aJson = new JSONArray(sJson);
List<Productos> prods = new ArrayList<Productos>();
for(int i=0; i<aJson.length(); i++) {
JSONObject json = aJson.getJSONObject(i);
Productos prod = new Productos();
prod.setProduct(json.getString("name"));
prod.setPrice(json.getString("price"));
JSONArray imgsJson = json.optJSONArray("images");
JSONObject images = new JSONObject(imgsJson.toString());
JSONArray image = images.getJSONArray("image");
for(int j = 0 ; j < image.length(); j++){
JSONObject img = image.getJSONObject(i);
prod.setImage(img.getString("image_medium"));
}
prod.setId(Integer.valueOf(json.getString("id")));
prods.add(prod);
}
提前致谢。