保存JSON的例外情况

时间:2015-05-29 12:26:03

标签: android android-activity android-json

我正在尝试保存服务器的响应,但我得到了例外。 这是回复:

SELECT * FROM items i 
LEFT JOIN item_tags it ON i.item_id = it.item_id
LEFT JOIN tags t ON t.tag_id = it.tag_id
WHERE tag_title = 'x'

我无法保存。我在想的是:

   [ 
    {
    "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();

            }

我真的很困惑这么多数组和对象请帮忙,thnxx

5 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:0)

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

在您的情况下,正确的JSON应该看起来像那样:

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

答案 2 :(得分: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");

答案 3 :(得分:0)

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

答案 4 :(得分:0)

[] = Json数组和{} = Json对象。您可以通过其键来读取json对象,例如'CustomerId','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

等库