I am trying to parse this JSON Output:
{
"answers": [
{
"table_data": [
[
{
"column_id": 49,
"value": "1957"
}
],
[
{
"column_id": 49,
"value": "1836"
}
]
]
},
{
"table_data": {
"0": [
{
"column_id": 61,
"value": "1091"
},
{
"column_id": 62,
"value": "2046"
}
],
"1": [
{
"column_id": 61,
"value": "467"
},
{
"column_id": 62,
"value": "1429"
}
],
"2": [
{
"column_id": 61,
"value": "1236"
},
{
"column_id": 62,
"value": "2202"
}
]
}
}
]
}
using this code:
groups = json.getJSONArray(TAG_ANSWERS);
for (int i = 0; i < groups.length(); i++) {
JSONObject c = groups.getJSONObject(i);
ansTable = c.getString(TAG_ANSWER_TABLE);
System.out.println("TABLE: " + ansTable);
//test if there is "0" or an array
JSONArray arr = c.getJSONArray(TAG_ANSWER_TABLE); ****ERROR HERE****
System.out.println("ARRAY: " + String.valueOf(arr));
for (int j = 0; j <arr.length();j++){
JSONArray child = arr.getJSONArray(j);
JSONObject d = child.getJSONObject(0);
String col = d.getString(TAG_ANSWER_TABLE_COLID);
String val = d.getString(TAG_ANSWER_TABLE_VALUE);
System.out.println("DATA: " + col +": "+val);
System.out.println("CHILD: " + String.valueOf(child));
System.out.println("INNER: " + String.valueOf(d));
}
The above piece of code correctly parses the first table_data
but throws a type org.json.JSONObject cannot be converted to JSONArray
error as indicated on the piece of code above.
How would I correctly parse the 0, 1, 2 etc? I know they're objects but I'm not sure how I would test for the occurrence of them. Any help would be greatly appreciated. Thanks