2015-05-29 32 views
-1

我试图挽救服务器的响应,但我得到的例外.. 这是响应:异常节省JSON

[ 
    { 
    "MediEazyInvoiceitemsList": [ 
     { 
      "Id": 1, 
      "MediEazyInvoiceId": 1, 
      "ProductId": 1, 
      "ManufacturerId": null, 
      "ProductName": "Crocef (500 mg)", 
      "ScheduleType": "H", 
      "Quantity": 2, 
      "Dosage": "1-0-1" 
     }, 
     { 
      "Id": 2, 
      "MediEazyInvoiceId": 1, 
      "ProductId": 1, 
      "ManufacturerId": null, 
      "ProductName": "Dispar (300 mg)", 
      "ScheduleType": "H", 
      "Quantity": 5, 
      "Dosage": "1-0-1" 
     } 
    ], 
    "Id": 1, 
    "CustomerId": 5, 
    "PharmacyId": 1, 
    "Customer": null, 
    "DeliveryAddress": "sfh, ghh", 
    "CustomerLatitude": "24.9876", 
    "CustomerLongitude": "72.0987", 
    "OrderStatus": 0, 
    "Remarks": null, 
    "Comments": null, 
    "Instructions": "Testing", 
    "Prescription": null, 
    "PrescriptionPath": "" 
}, 
{ 
    "MediEazyInvoiceitemsList": [ 
     { 
      "Id": 3, 
      "MediEazyInvoiceId": 2, 
      "ProductId": 1, 
      "ManufacturerId": null, 
      "ProductName": "Crocin (15 ml)", 
      "ScheduleType": "H", 
      "Quantity": 1, 
      "Dosage": "1-0-1" 
     }, 
     { 
      "Id": 4, 
      "MediEazyInvoiceId": 2, 
      "ProductId": 1, 
      "ManufacturerId": null, 
      "ProductName": "Dispar (300 mg)", 
      "ScheduleType": "H", 
      "Quantity": 5, 
      "Dosage": "1-0-1" 
     } 
    ], 
    "Id": 2, 
    "CustomerId": 5, 
    "PharmacyId": 1, 
    "Customer": null, 
    "DeliveryAddress": "sfh, ghh", 
    "CustomerLatitude": "24.9876", 
    "CustomerLongitude": "72.0987", 
    "OrderStatus": 0, 
    "Remarks": null, 
    "Comments": null, 
    "Instructions": "Testing", 
    "Prescription": null, 
    "PrescriptionPath": "" 
}] 

我救不了这一点。我试图为:

String result = Utils.convertInputStreamToString(inputStream); 

      //Printing server response 
      System.out.println("server response is :" + result + "\n" + inputStream); 


      try { 
       JSONObject jsonObject=new JSONObject(); 
       JSONObject jo=jsonObject.getJSONObject(""); 
       ja=jo.getJSONArray("MediEazyInvoiceitemsList"); 

       // checking if server response is successful 


      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

      } 

这是我收到的错误:

05-29 17:53:51.714: W/System.err(8891): org.json.JSONException: No value for MediEazyInvoiceItemsList 

我有这么多的阵列真的很困惑和对象请大家帮忙,thnxx

+0

的JSONObject的JSONObject =新的JSONObject();应该是JSONObject jsonObject = new JSONObject(myStringVariableWithJSONData); –

+0

'JSONObject jsonObject = new JSONObject(response);' –

+0

我不知道myStringVariableWithJSONData ... – Prakhar

回答

0

您的JSON结构是一个包含两个对象的数组,每个对象都有一个"MediEazyInvoiceitemsList"键。两个不如意的事情:1)你是不是在JSON字符串传递和2)你想找回它不是位于“MediEazyInvoiceItemsList”键。

第一遍的JSON字符串。例如,如果服务器响应位于名为jsonString的字符串中:

JSONArray jsonArray = new JSONArray(jsonString); 

然后循环并检索所需的对象或数组。您的结构并不复杂,但您必须确保您正确使用JSONArray或JSONObject。例如,为了直接拿到项目的第一清单:

// Retrieve the first object. 
JSONObject firstObject = jsonArray[0]; 

// Retrieve the property that holds the first list of items. 
JSONArray firstListOfItems = firstObject.getJSONArray("MediEazyInvoiceitemsList"); 
0

这是inproperly格式化JSON,键不应该是引号,如"MediEazyInvoiceitemsList"

正确的JSON在你的情况下应该看起来像这样:

[ //JSONArray 
    { //JSONObject, first 
    MediEazyInvoiceitemsList: [ 
     { 
      Id: 1, 
      MediEazyInvoiceId: 1, 
      ProductId: 1, 
      ManufacturerId: null, 
      ProductName: "Crocef (500 mg)", 
... etc. 
1

试试这个

JSONArray jsonArray=new JSONArray(result); 
JSONObject jObject=jsonArray.getJSONObject(0); 
JSONArray jsonResponse = jsonObject.getJSONArray("MediEazyInvoiceItemsList"); 
+0

我想这一点,它说无效指数 – Prakhar

+0

在你的代码,你不传递任何JSONARRAY..is指数,也响应您的最终代码 – Meenal

+0

also..your主要目的在于JSONArray不JSONObject的 – Meenal

0
 try { 
       JSONArray jsonArray=new JSONArray(response); 
       JSONObject jsonObject=jsonArray.getJSONObject(0); 
       JSONArray jsArry=jsonObject.getJSONArray("MediEazyInvoiceitemsList"); 
       .... 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
+0

nosuchmethod错误....在我的代码,回复是JSONResponse – Prakhar

+0

发表日志,它是哪种方法。 –

0

[] = JSON数组和和{} = JSON对象。您可以通过像 '客户编号' 关键看JSON对象, 'ID', '是DeliveryAddress' ...... 试试这个

try { 
     JSONArray responseArray = new JSONArray(yourRequestResponse); 
     for (int i = 0; i < responseArray.length(); i++) { 
      JSONObject object = responseArray.getJSONObject(i); 
      String customerId=object.getString("CustomerId"); //product details 

      //reading items details (ie array) 
      JSONArray mediInvcList = object.getJSONArray("MediEazyInvoiceitemsList"); 
      for (int j = 0; j < mediInvcList.length(); j++) { 
       JSONObject item=mediInvcList.getJSONObject(j); 
         int id=item.getInt("Id"); 
         //same for others 
      } 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

建议你使用的库一样GSON