I have the following json that i'm trying to parse without succeed:
[{
"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"
}
}]
}
}]
I need to get the name, id, price and all the images data as well i've searched and googled with no exit, that's why i ended here because normally i found all the answers to my questions in here but not this time.
This is the code i'm using to parse the data now, i'm able to get name, id, price but had no luck with images.
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);
}
Thanks in advance.