无法在android中解析json数组

时间:2014-09-13 05:29:14

标签: android json parsing

我正在尝试解析以下Json数组。我已经完成了以下编码,但由于我遇到异常,我无法正确解析它。请检查我的密码并告诉我哪里出错了。

String result = null;
        InputStream is = null;
        try{
                HttpClient httpclient = new DefaultHttpClient();

                HttpPost httppost = new HttpPost("url");


                HttpResponse response = httpclient.execute(httppost); 

                HttpEntity entity = response.getEntity();
                is = entity.getContent();

                Log.e("log_tag", "connection success "+"nameValuePairs");

        }
        catch(Exception e)
        {
                Log.e("log_tag", "Error in http connection "+e.toString());


        }

        try
        {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,HTTP.UTF_8),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) 
                {
                        sb.append(line + "\n");

                }
                is.close();

                result=sb.toString();
              Log.i("log_tag", "result "+result.toString());
        }
        catch(Exception e)
        {
               Log.e("log_tag", "Error converting result "+e.toString());


        }

        try
        {

            JSONArray jArray=new JSONArray(result);
        Log.i("json array", ""+jArray);
            String s="",s1,s2,s3,s4,s5,s6,s7,s8,s9;


          JSONArray newarr = jArray.getJSONObject(0).getJSONArray("order_item_list");
          JSONArray newarr2 = jArray.getJSONObject(0).getJSONArray("order_course_list");
          JSONArray newarr3 = jArray.getJSONObject(0).getJSONArray("order_item_list");

          ArrayList<HashMap<String, String>>val=new ArrayList<HashMap<String,String>>();

          for(int i=0;i<newarr.length();i++)
          {
              HashMap<String, String> map=new HashMap<String, String>();
              String p,p1,p2,p3,p4,p5;
              JSONObject json_data = newarr.getJSONObject(i);
                p=json_data.getString("order_id"); 



          }

       catch(JSONException e)
        {
                Log.e("log_tag", "Error parsing data "+e.toString());

        }

Json结果字符串

{
    "order_list": [
        {
            "order_course_list": [
                {
                    "order_item_list": [
                        {
                            "food_Id": "35",
                            "food_item_id": "18",
                            "food_name": "OrientalSalad",
                            "food_price": "8",
                            "food_quantity": "120",
                            "food_item_price": "8",
                            "food_image": "http: //166.62.17.208//images_large/oriental-salad-new.jpg",
                            "food_prefids": "Nil",
                            "food_preference": "",
                            "food_preference_price": "",
                            "ItemStatus": ""
                        },
                        {
                            "food_Id": "35",
                            "food_item_id": "19",
                            "food_name": "OrientalSalad",
                            "food_price": "6",
                            "food_quantity": "90",
                            "food_item_price": "6",
                            "food_image": "http: //166.62.17.208//images_large/oriental-salad-new.jpg",
                            "food_prefids": "@15@13",
                            "food_preference": "@NoRanchSauce@ExtraCroutons",
                            "food_preference_price": "@0@0",
                            "ItemStatus": ""
                        },
                        {
                            "food_Id": "45",
                            "food_item_id": "20",
                            "food_name": "CremeBrulee",
                            "food_price": "3",
                            "food_quantity": "36",
                            "food_item_price": "3",
                            "food_image": "http: //166.62.17.208//MenuImage.aspx?imgId=5555_23",
                            "food_prefids": "Nil",
                            "food_preference": "",
                            "food_preference_price": "",
                            "ItemStatus": ""
                        },
                        {
                            "food_Id": "42",
                            "food_item_id": "26",
                            "food_name": "Lasagna",
                            "food_price": "3",
                            "food_quantity": "156",
                            "food_item_price": "3",
                            "food_image": "http: //166.62.17.208//images_large/lasagna.jpg",
                            "food_prefids": "@22",
                            "food_preference": "@ExtraCheese",
                            "food_preference_price": "@0",
                            "ItemStatus": ""
                        },
                        {
                            "food_Id": "42",
                            "food_item_id": "27",
                            "food_name": "Lasagna",
                            "food_price": "4",
                            "food_quantity": "208",
                            "food_item_price": "4",
                            "food_image": "http: //166.62.17.208//images_large/lasagna.jpg",
                            "food_prefids": "@22",
                            "food_preference": "@ExtraCheese",
                            "food_preference_price": "@0",
                            "ItemStatus": ""
                        },
                        {
                            "food_Id": "40",
                            "food_item_id": "29",
                            "food_name": "Falafel",
                            "food_price": "2",
                            "food_quantity": "10",
                            "food_item_price": "2",
                            "food_image": "http: //166.62.17.208//images_large/falafel.jpg",
                            "food_prefids": "@32@33",
                            "food_preference": "@ServedwithHammous@ServedwithGarlicpaste",
                            "food_preference_price": "@0@0",
                            "ItemStatus": ""
                        },
                        {
                            "food_Id": "47",
                            "food_item_id": "30",
                            "food_name": "ChocolateMousse",
                            "food_price": "3",
                            "food_quantity": "27",
                            "food_item_price": "3",
                            "food_image": "http: //166.62.17.208//MenuImage.aspx?imgId=5555_25",
                            "food_prefids": "Nil",
                            "food_preference": "",
                            "food_preference_price": "",
                            "ItemStatus": ""
                        },
                        {
                            "food_Id": "51",
                            "food_item_id": "31",
                            "food_name": "ShishTawook",
                            "food_price": "2",
                            "food_quantity": "34",
                            "food_item_price": "2",
                            "food_image": "http: //166.62.17.208//MenuImage.aspx?imgId=5555_29",
                            "food_prefids": "@30",
                            "food_preference": "@MediumFried",
                            "food_preference_price": "@0",
                            "ItemStatus": ""
                        },
                        {
                            "food_Id": "42",
                            "food_item_id": "34",
                            "food_name": "Lasagna",
                            "food_price": "5",
                            "food_quantity": "260",
                            "food_item_price": "5",
                            "food_image": "http: //166.62.17.208//images_large/lasagna.jpg",
                            "food_prefids": "@22@23",
                            "food_preference": "@ExtraCheese@ExtraMeat",
                            "food_preference_price": "@0@0",
                            "ItemStatus": ""
                        },
                        {
                            "food_Id": "50",
                            "food_item_id": "35",
                            "food_name": "ShawermaPlatter",
                            "food_price": "1",
                            "food_quantity": "14",
                            "food_item_price": "1",
                            "food_image": "http: //166.62.17.208//MenuImage.aspx?imgId=5555_28",
                            "food_prefids": "@27",
                            "food_preference": "@ExtraMeat",
                            "food_preference_price": "@0",
                            "ItemStatus": ""
                        },
                        {
                            "food_Id": "51",
                            "food_item_id": "36",
                            "food_name": "ShishTawook",
                            "food_price": "1",
                            "food_quantity": "17",
                            "food_item_price": "1",
                            "food_image": "http: //166.62.17.208//MenuImage.aspx?imgId=5555_29",
                            "food_prefids": "Nil",
                            "food_preference": "",
                            "food_preference_price": "",
                            "ItemStatus": ""
                        }
                    ],
                    "sub_id": "18",
                    "master_order_id": "16",
                    "status": "Placed",
                    "placed_time": "9/9/201412: 42: 51PM",
                    "confirmed_time": "",
                    "processed_time": "",
                    "waiter_code": "",
                    "order_type": "1",
                    "item_processed_status": ""
                }
            ],
            "order_id": "16",
            "initial_time": "9/3/20141: 02: 45PM",
            "placed_time": "9/9/20142: 57: 01PM",
            "confirmed_time": "",
            "processed_time": "",
            "paid_time": "",
            "total": "38",
            "guest_order_type": "Home",
            "order_status": "Placed",
            "payment_satus": "NotPaid",
            "delivery_time": "",
            "status": "Placed",
            "table_no": "1",
            "order_type": "",
            "device_type": ""
        }
    ]
}

3 个答案:

答案 0 :(得分:1)

你得到的第一个地方json对象不是数组。 尝试

    JsonObject jsonobject = new JsonObject(result);
JSONArray jArray=jsonobject.getJsonArray("order_list");

答案 1 :(得分:1)

你的Json将对象作为字符串而不是数组返回..

在这里解析你的JSON并获得更好的解决方案...... jsoneditoronline

试试以下...

JSONObject JObj=new JSONObject (jsonresult);

JSONArray order_list = JObj.getJSONArray("order_list");
JSONArray order_course_list= order_list.getJSONObject(0).getJSONArray("order_course_list");
JSONArray order_item_list= order_course_list.getJSONObject(0).getJSONArray("order_item_list");


//getting order Id from **order_list array**

for(int i=0;i<order_list.length();i++)
{
         String order_id = order_list.getJSONObject(i).getString("order_id");
}

答案 2 :(得分:1)

您编写了错误的代码,这就是您无法解析此json的原因。在尝试解析json之前读取任何json文档。

JsonObject jsonobject = new JsonObject(result);    
JSONArray newarr = jsonobject. getJsonArray("order_list");
      JSONArray newarr2 = newarr.getJSONObject(0).getJSONArray("order_course_list");
      JSONArray newarr3 = newarr2.getJSONObject(0).getJSONArray("order_item_list");

      ArrayList<HashMap<String, String>>val=new ArrayList<HashMap<String,String>>();

      for(int i=0;i<newarr2.length();i++)
      {
          HashMap<String, String> map=new HashMap<String, String>();
          String p,p1,p2,p3,p4,p5;
          JSONObject json_data = newarr2.getJSONObject(i);
          p=json_data.getString("order_id");
      }