如何在不知道JSONObject和JSONArray名称和结构的情况下动态解析完整的JSON数据?

时间:2018-04-12 12:58:15

标签: android json

我想在不知道任何名称和结构的情况下获取JSON数据。实际上,我们有多个表名作为(数组名称),列名称作为(键,值),我希望动态获取它,并根据解析的表名称将解析后的列(键)上的解析数据(值)存储到SQLite数据库中(数组名称)。

这个JSON数据是演示数据,表名可以超过20个,但数据是这样的,所以请帮我解决这个问题。

{
  "MSG": "OK",
  "data": [
    {
      "strPrimaryKey": "iDeviceAppId",
      "Device_App": [
        {
          "strAppName": "NI Data Dashbard",
          "isDeleted": "0",
          "strVersion": "2.3.0",
          "dtCreateDate": "2018-04-09",
          "iDeviceAppId": "0",
          "dtUpdateDate": "2018-04-11",
          "iOnHomeScreen": "1"
        }
      ]
    },
    {
      "strPrimaryKey": "iDeviceAppId",
      "Table_App": [
        {
          "strAppName": "NI Data Dashbard",
          "isDeleted": "0",
          "strVersion": "1.8.0",
          "dtCreateDate": "2018-04-09",
          "iDeviceAppId": "0",
          "dtUpdateDate": "2018-04-11",
          "iOnHomeScreen": "1"
        }
      ]
    },
    {
      "strPrimaryKey": "iDeviceAppId",
      "Device_App": [
        {
          "strAppName": "NI Data Dashbard",
          "isDeleted": "0",
          "strVersion": "2.3.0",
          "dtCreateDate": "2018-04-09",
          "iDeviceAppId": "0",
          "dtUpdateDate": "2018-04-11",
          "iOnHomeScreen": "1"
        }
      ]
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

这是它的解决方案

String data = "{ ... }";
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
  //you have an object
else if (json instanceof JSONArray)
  //you have an array

在运行时动态使用迭代器

知道密钥