我有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 :(得分:0)
如何解析JSON及其中的内容遵循here。
答案 1 :(得分: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
}
使用try catch环绕异常....
答案 2 :(得分:0)
首先,如果您的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
答案 3 :(得分:0)
Json解析就是要理解对象的层次结构&#39; {}&#39;和阵列&#39; []&#39; 。您可以查看以下链接以了解它。 How to parse this nested JSON array in android 或http://www.coderzheaven.com/2011/06/10/complex-json-string-parsing-in-android/