Reputation: 403
My JSON data looks like this:
[
{
"$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"
}
]
and my code looks like this:
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();
}}}
My JSON data comes like charm when API is called but getString()
doesn't return anything. Also tried optString()
Edit:I tried getting an array but it is really confusing
Upvotes: 0
Views: 305
Reputation: 244
Here, your response starts with an array. So, first of you need to get an array from the response.
JSONArray jsonArray = new JSONArray(response);
JsonArray blockArray = getJSONObject(i).getString("Blocks");
String status = blockArray.get(0).getString("Status");
Upvotes: 1