JSONObject.getString(“Value”)不返回JSON字符串

时间:2018-05-04 13:15:19

标签: android json

我的JSON数据如下所示:

[
  {
    "$id": "1",
    "Blocks": [
      {
        "$id": "2",
        "Thing": {
          "$ref": "1"
        },
        "Id": 70020,
        "Index": 6,
        "PreviousHash": "12BF3BAA7E8B4055D0FD7070FB53B217EE2F427E52B7BBE8F7434194E1C04B",
        "Timestamp": "2018-03-30T17:13:41.99",
        "Hash": "676F1147C73964A1125F374EE9DA58469B25C67F1A70C8DCBC2B8B7695E7416",
        "Status": "Valid",
        "ThingId": 6021
      }
    ],
    "Id": 6021,
    "Created": "2018-01-01T00:00:00",
    "Issuer": "Islamic University",
    "Program": "Elecrical Eng",
    "DateAwarded": "2000-01-01T00:00:00",
    "CGPA": 4,
    "Name": "Danish.S",
    "CNIC": "777777",
    "LinkedIn": "ffff",
    "LastAction": "record",
    "QRCode": "3YCA8J33ZY"
  }
]

我的代码如下:

   StringRequest stringRequest = new StringRequest(Request.Method.GET, uri,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject serverResp = new JSONObject(response.toString());
                                String auth = serverResp.optString("Status");
                                txtResult.setText(auth);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }}}

调用API时,我的JSON数据就像魅力,但getString()不会返回任何内容。还尝试了optString()

编辑:我尝试了一个阵列,但实在令人困惑

1 个答案:

答案 0 :(得分:1)

在这里,您的响应以数组开头。所以,首先需要从响应中获取一个数组。

JSONArray jsonArray = new JSONArray(response);
JsonArray blockArray = getJSONObject(i).getString("Blocks");
String status = blockArray.get(0).getString("Status");