保存异常 JSON

Exception in saving 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

格式不正确 JSON,键不应该在引号中,例如 "MediEazyInvoiceitemsList"

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

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

您的 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");

试试这个

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

[]=Json 数组和 {} = Json 对象。您可以通过 'CustomerId'、'Id'、'DeliveryAddress'...等键读取 json 对象...试试这个

    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

这样的库