简体   繁体   中英

how can parse complete JSON data dynamically without knowing JSONObject and JSONArray name and structure?

I want to get the JSON data without knowing any name and structure. Actually, we have multiple table name as (array name) and column name as(key, value) and I want to get it dynamically and store parsed data(values) on parsed columns(keys) into SQLite database according to parsed table name(array name).

This JSON data is demo data and table name can be more than 20 but data is coming like this, so please help me to solve this problem.

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

this the solution for it

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

And to know the key at run-time dynamically use iterator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM