2016-07-26 107 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