Reputation: 476
So I'm trying to get the 'statusSeveretyDescription' which is inside 'lineStatus'. The first 'for' loop works, however, the nested loop returns null.. A pointer in the right direction would be much appreciated. Please see code and an image of the JSON below.
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.optJSONObject(i);
String line = object.optString("id");
//nested array
JSONArray arrayStatus = object.getJSONArray("lineStatuses");
int len = arrayStatus.length();
for (int j = 0; j < len; j++) {
JSONObject o = arrayStatus.getJSONObject(j);
String statusSeverityDescription = o.optString(
"statusSeverityDescription", "");
}
if (line != null) {
tubeLines.add(line + statusSeverityDescription);
}
// Once we added the string to the array, we notify the arrayAdapter
arrayAdapter.notifyDataSetChanged();
}
Upvotes: 1
Views: 432
Reputation: 3260
You have 2 issues.
1) Your variable scoping is incorrect, your statusSeverityDescription
variable is being deallocated as soon as the for loop ends. To fix this, move String statusSeverityDescription
outside of the for loop, like so
String statusSeverityDescription;
for (int j = 0; j < len; j++) {
JSONObject o = arrayStatus.getJSONObject(j);
statusSeverityDescription = o.optString(
"statusSeverityDescription", "");
}
2) statusSeverityDescription
should be an array which store all the statusSeverityDescription
's in your json payload. So make it an array, or understand that the last processed statusSeverityDescription
for a given lineStatuses
will be the statusSeverityDescription
you're left with.
Upvotes: 2