-1

I am trying to save the response from server but I am getting exception.. This is the response:

   [ 
    {
    "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": ""
}]

I can't save this. What I am trying is:

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();

            }

This is the error I am getting:

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

I am really confused with so many arrays and objects please help, thnxx

Prakhar
  • 710
  • 6
  • 24

5 Answers5

1

try this

JSONArray jsonArray=new JSONArray(result);
JSONObject jObject=jsonArray.getJSONObject(0);
JSONArray jsonResponse = jsonObject.getJSONArray("MediEazyInvoiceItemsList");
Meenal
  • 2,879
  • 5
  • 19
  • 43
0

this is inproperly formated JSON, keys shouldn't be in inverted commas, like "MediEazyInvoiceitemsList"

proper JSON in your case should look smth like that:

[ //JSONArray
    { //JSONObject, first
    MediEazyInvoiceitemsList: [
        {
            Id: 1,
            MediEazyInvoiceId: 1,
            ProductId: 1,
            ManufacturerId: null,
            ProductName: "Crocef (500 mg)",
... etc.
snachmsm
  • 17,866
  • 3
  • 32
  • 74
0

Your JSON structure is one array containing two objects, each with one "MediEazyInvoiceitemsList" key. Two things go wrong: 1) you are not passing in the JSON string and 2) you are trying to retrieve the "MediEazyInvoiceItemsList" key where it is not located.

First pass in the JSON string. For example, if the server response is in a String named jsonString:

JSONArray jsonArray = new JSONArray(jsonString);

Then loop it and retrieve the objects or arrays you need. Your structure is not that complicated, but you have to make sure you correctly use JSONArray or JSONObject. For instance, to directly get the first list of items:

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

// Retrieve the property that holds the first list of items.
JSONArray firstListOfItems = firstObject.getJSONArray("MediEazyInvoiceitemsList");
Jacob Ras
  • 5,974
  • 1
  • 28
  • 26
0
     try {
                JSONArray jsonArray=new JSONArray(response);
                JSONObject jsonObject=jsonArray.getJSONObject(0);
                JSONArray jsArry=jsonObject.getJSONArray("MediEazyInvoiceitemsList");
                ....
            } catch (JSONException e) {
                e.printStackTrace();
            }
Krishna V
  • 1,801
  • 1
  • 14
  • 16
0

[]=Json array and and {} = Json Object. You can read json object by its key like 'CustomerId' , 'Id', 'DeliveryAddress'... try this

    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();
    }

Suggest you to use library like GSON

Bharatesh
  • 8,943
  • 3
  • 38
  • 67