1

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

mhorgan
  • 886
  • 2
  • 11
  • 32

2 Answers2

1

The data structure is inconsitent, the 2nd part below is not array but object, but the first table_data is array

       "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"
                }
            ]
        }

Edit:

JSONArray arr = c.optJSONArray(TAG_ANSWER_TABLE);
if (arr!=null) {
   //parse as array
} else {
    //you may use c.getJSONObject(TAG_ANSWER_TABLE); as well, if it MUST be either array or object
    JSONObject obj = c.optJSONObject(TAG_ANSWER_TABLE); 
   //parse as obj

}
Derek Fung
  • 8,171
  • 1
  • 25
  • 28
  • Is there any way to parse both? Or would I need both to be either an array/ object? – mhorgan Sep 14 '15 at 13:51
  • 1
    `JSONArray arr = c.optJSONArray(TAG_ANSWER_TABLE);` you should use this and then check whether `arr` is null, if it is null, you check `JSONObject obj = c.optJSONObject(TAG_ANSWER_TABLE);` to get and parse it as object – Derek Fung Sep 14 '15 at 13:57
  • That's helpful. I found out a couple of minutes ago that `opt` is better in this case as it returns `null` if the object doesn't exist. – mhorgan Sep 14 '15 at 13:58
  • 1
    Yes. I have updated my answer to include that as well – Derek Fung Sep 14 '15 at 14:02
-1
{
    "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"
                    }
                ]
            }
        }
    ]
}

The above JSON is not well-formatted,

1. Check the "answer" object index (0) "table_data" is a JSONArray, but on index (1) "table_data" is a JSONObject.

2. When "table_data" is Array it doesn't contain the keys but when "table_data" is object it contains keys like "0", "1", "2" etc

Conclusion

You can not parse it because it is not consistent...!
Arsal Imam
  • 2,882
  • 2
  • 24
  • 35
  • It is parseable still no? Can't I just: `JSONObject obj = c.optJSONObject(TAG_ANSWER_TABLE); if (obj == null) { //parse array} else {//parse object}` – mhorgan Sep 14 '15 at 14:02