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.

有帮助吗?

解决方案

Use either Jackson or Gson for this. They will allow you to supply a POJO to return as. This makes it a whole lot easier than defining JSonArray's. Additionally assuming you are making a web request, I recommend using RoboSpice and Spring for Android for a more robust experience.

Links:

http://wiki.fasterxml.com/JacksonDownload

https://code.google.com/p/google-gson/

https://github.com/octo-online/robospice

其他提示

  • With Gson library is really simple

    List<ProductosClass> yourClassList = new Gson().fromJson(jsonArray, ProductosType);
    

Check http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/Gson.html:

  • You can also use Jackson
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top