-1

I have JSON response and it have JSON array inside JSON array. Please tell me how to parse it. and please tell me it's best example for multiple JSON array parsing. I have all field dynamic so developer give me this kind of response. Thanks in advance.

    {
"products": [
    {
      "id": "14",
      "cat_id": [
        "1"
      ],
      "category_name": [
        "Meetha Paan Sall Supari"
      ],
      "name": "Chocco Paan",
      "product_image": "http://freedemo.info/paanvaala/php/media/product_image/t0xdfosjr_1_173_201.png",
      "price_unit": "in_pcs",
      "price_id": [
        "14"
      ],
      "weight": [
        "1 PCS"
      ],
      "price": [
        150
      ],
      "mrp": [
        "150.00"
      ],
      "description": null
    },
    {
      "id": "13",
      "cat_id": [
        "1"
      ],
      "category_name": [
        "Meetha Paan Sall Supari"
      ],
      "name": "Chochobar Pan",
      "product_image": "http://freedemo.info/paanvaala/php/media/product_image/hul6e69n6_1_173_201.png",
      "price_unit": "in_pcs",
      "price_id": [
        "13"
      ],
      "weight": [
        "1 PCS"
      ],
      "price": [
        85
      ],
      "mrp": [
        "85.00"
      ],
      "description": null
    },
  ],
}
Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64

4 Answers4

0

How to Parse JSON and what its has in it Follow here.

enter image description here

Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

parse json:

JSONObject jsonObj = new JSONObject(jsonStr);// your string.
JSONArray jsonArray = jsonObj.getJSONArray("products");


for(int i=0; i < jsonArray.length(); i++){
       JSONObject prod = jsonArray.getJSONObject(i);
       String id = prod.getString("id");

       // and so on ...you can parse the entire json using either array or object

}

surround with try catch for exception....

Riad
  • 3,822
  • 5
  • 28
  • 39
0

First you need to verif y if your JSON is valid or not. Json Validator

if you find this valid then `

try { JSONObject  jsonRootObject = new JSONObject(strJson);
         //Get the instance of JSONArray that contains JSONObjects
         JSONArray jsonArray = jsonRootObject.optJSONArray("products");   
         //Iterate the jsonArray and print the info of JSONObjects
         for(int i=0; i < jsonArray.length(); i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            int id = Integer.parseInt(jsonObject.optString("id").toString());
            String name = jsonObject.optString("name").toString();
            float product_image = Float.parseFloat(jsonObject.optString("product_image").toString());
         JSONArray cat_idArray = jsonRootObject.optJSONArray("cat_id");    
         for(int i=0; i < cat_idArray.length(); i++){
        //Here you can get other values
           }
            data += "Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n Salary= "+ salary +" \n ";
         }
         output.setText(data);
      } catch (JSONException e) {e.printStackTrace();}

` Also there are lots of example in web where you can learn. like this one

Abhishek Kumar
  • 236
  • 1
  • 10
0

Json parsing is all about understanding the hierarchy of objects '{}' and array '[]' . you can look at following links to understand it. How to parse this nested JSON array in android or http://www.coderzheaven.com/2011/06/10/complex-json-string-parsing-in-android/

Community
  • 1
  • 1
Abhishek Seth
  • 93
  • 1
  • 8