应该在JSON中引用值以及键

时间:2017-05-29 06:01:02

标签: java android json

我有数据 像这样:

{
    "status": "success",
    "message": "Student Statement Report",
    "data": {
        "data": [{
            "id": "45",
            "transaction_no": "45",
            "transaction_date": "2017-05-25",
            "transaction_type": "invoice",
            "transaction_amount": "1010.00",
            "related_invoice_id": "45",
            "balance_amount": "1010.00",
            "related_user_id": "436",
            "related_user_group": "student",
            "description": "",
            "created_by": "Principal",
            "updated_by": "Principal",
            "created_at": "2017-05-25 11:57:39",
            "updated_at": "2017-05-25 11:57:39"
        }],
        "opening_balance": 0,
        "dates": ["2017-05-22 00:00:00", "2017-05-28 23:59:59"]
    }
}


                  JSONObject jsonObject = new JSONObject(response);

我正在从这里打出Json预期错误                           String openingBalance = jsonObject.getString(" opening_balance");

" opening_balance":0, 所以,我最大的问题是应该引用零(值)吗?

3 个答案:

答案 0 :(得分:1)

你必须像这样解析它:

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
Uri deleteUri = null;
deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, Long.parseLong(eventID));
int rows = getContentResolver().delete(deleteUri, null, null);

答案 1 :(得分:0)

如果您将数字读作整数,则无需报价。但如果您将其读作字符串,那么您必须加上引号。

要读作整数,您可以使用getInt("json_key") &安培;对于字符串 getString("json_key")

答案 2 :(得分:0)

JSONObject jsonObject = new JSONObject(response);
JSONObject data = jsonObject.getJSONObject("data");//Get Data object
int openingBalance = data.getInt("opening_balance");//Get opening balance