-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

7
  • JSONObject jsonObject=new JSONObject(); should be JSONObject jsonObject=new JSONObject(myStringVariableWithJSONData); Commented May 29, 2015 at 12:27
  • JSONObject jsonObject=new JSONObject(response);
    – M D
    Commented May 29, 2015 at 12:27
  • I dont know myStringVariableWithJSONData...
    – Prakhar
    Commented May 29, 2015 at 12:31
  • @MD response is the HttpResponse and it says undefined for the type httpresponse
    – Prakhar
    Commented May 29, 2015 at 12:32
  • First clear your question with some more details or you should go How to ask
    – M D
    Commented May 29, 2015 at 12:34

5 Answers 5

1

try this

JSONArray jsonArray=new JSONArray(result);
JSONObject jObject=jsonArray.getJSONObject(0);
JSONArray jsonResponse = jsonObject.getJSONArray("MediEazyInvoiceItemsList");
2
  • in your code you are not passing any index and also response in JSONARRAY..is that your final code
    – Meenal
    Commented May 29, 2015 at 12:45
  • also..your main object is JSONArray not JSONObject
    – Meenal
    Commented May 29, 2015 at 12:45
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");
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.
0
     try {
                JSONArray jsonArray=new JSONArray(response);
                JSONObject jsonObject=jsonArray.getJSONObject(0);
                JSONArray jsArry=jsonObject.getJSONArray("MediEazyInvoiceitemsList");
                ....
            } catch (JSONException e) {
                e.printStackTrace();
            }
1
  • post that log, which method it is.
    – Krishna V
    Commented May 29, 2015 at 12:48
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.