2016-07-26 104 views
-1

我有JSON响应,它有JSON数组内部的JSON数组。请告诉我如何解析它。并请告诉我这是多个JSON数组解析的最佳示例。我拥有所有的领域动态,所以开发人员给我这种回应。提前致谢。我如何解析JSON响应

{ 
"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 
    }, 
    ], 
} 
+0

此问题可能受益于其他格式和说明。你的用例是什么?你想从中提取哪些数据? – adeora

+1

谷歌搜索之前,你发布,有例子的音调 –

+0

很多的例子存在先试用它,你会得到更好的知识。 –

回答

0

如何解析JSON及其中的内容遵循here

enter image description here

+0

我想要解析多个json数组的示例 –

0

解析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 

} 

的环绕尝试捕捉异常....

0

首先你需要VERIFŸ如果你的JSON是有效还是无效。 Json Validator

如果你发现这个有效的,那么`

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();} 

` 也有很多例子在网上那里可以学到的。 like this one