Android JSON: 解析数组中的数组

Android JSON: Parse array in an array

我正在尝试解析这个 JSON 输出:

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

使用此代码:

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));
    }

上面的代码正确地解析了第一个 table_data,但抛出了 type org.json.JSONObject cannot be converted to JSONArray 错误,如上面的代码所示。

我将如何正确解析 0、1、2 等?我知道它们是对象,但我不确定如何测试它们的出现。任何帮助将不胜感激。谢谢

数据结构不一致,下面第二部分不是数组而是对象,但是第一个table_data是数组

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

编辑:

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

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

上面的JSON格式不对,

1. 检查 "answer" 对象索引 (0) "table_data" 是一个 JSON 数组,但索引 (1) "table_data" 是一个 JSON 对象。

2. 当 "table_data" 是数组时,它不包含键,但当 "table_data" 是对象时,它包含键,如“0”、“1”、“2”等

结论

你不能解析它,因为它不一致......!