How to parse JSON with any key on android?

后端 未结 1 1311
梦谈多话
梦谈多话 2020-12-21 19:11

I want to parse JSON with any key on Android. JSON data consists of any key, array or values. Here are JSON data and my working code. I want to put JSON data into a class by

相关标签:
1条回答
  • 2020-12-21 19:38

    Yes this is possible.

    Put the JSON you receive in a JSONObject. You can loop trough the keys and get the values out of it.

    Example:

    //Create json object from string
    JSONObject newJson = new JSONObject(json);
    
    // Get keys from json
    Iterator<String> panelKeys = newJson.keys();
    
    while(panelKeys.hasNext()) {
         JSONObject panel = newJson.getJSONObject(panelKeys.next()); // get key from list
         String id  = panel.getString("id");
         String number  = panel.getString("number");
    }
    

    I hope this is what you were looking for

    0 讨论(0)
提交回复
热议问题