2014-09-13 57 views
-1

我想解析下面的Json數組。我已經完成了以下編碼,但由於出現異常,我無法正確解析它。請檢查我的代碼,並告訴我哪裏出錯了。不能解析json數組android

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": "@[email protected]", 
          "food_preference": "@[email protected]", 
          "food_preference_price": "@[email protected]", 
          "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": "@[email protected]", 
          "food_preference": "@[email protected]", 
          "food_preference_price": "@[email protected]", 
          "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": "@[email protected]", 
          "food_preference": "@[email protected]", 
          "food_preference_price": "@[email protected]", 
          "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": "" 
     } 
    ] 
} 
+0

張貼你的json在這裏... – 2014-09-13 05:30:10

+0

你可以請發佈結果字符串嗎? – 2014-09-13 05:31:48

回答

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"); 
} 
1

你在第一個地方的JSON對象不數組。 嘗試

JsonObject jsonobject = new JsonObject(result); 
JSONArray jArray=jsonobject.getJsonArray("order_list"); 
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"); 
     }