0

Here, I'm trying to parse this JSON Object into model. But I'm stuck here with the dynamic field names. Here is the example of the JSON Object

{
    "1": {
        "state": {
            "on": false,
            "bri": 0,
            "hue": 0,
            "sat": 0,
            "effect": "none",
            "xy": [
                0,
                0
            ],
            "ct": 0,
            "alert": "none",
            "colormode": "hs",
            "reachable": false
        },
        "type": "Extended color light",
        "name": "Hue Lamp 1",
        "modelid": "LCT001",
        "manufacturername": "Philips",
        "uniqueid": "00:17:88:01:00:f4:5a:aa-0b",
        "swversion": "5.23.1.13452"
    },
    "2": {
        "state": {
            "on": false,
            "bri": 254,
            "hue": 8000,
            "sat": 200,
            "effect": "none",
            "xy": [
                0.5469,
                0.3819
            ],
            "ct": 500,
            "alert": "none",
            "colormode": "hs",
            "reachable": true
        },
        "type": "Extended color light",
        "name": "Hue Lamp 2",
        "modelid": "LCT001",
        "manufacturername": "Philips",
        "uniqueid": "00:17:88:01:00:f4:5c:55-0b",
        "swversion": "5.23.1.13452"
    }
}

There are these field "1","2" and so on. How to parse this into model ? Can anybody help ?

Updated : All i want to achieve is getting this "1", "2" key value and the "name" field value in it. Thanks before !

Tommy Wu
  • 652
  • 7
  • 12

2 Answers2

0

You need to wrap json in one root element then use model like this

public class RootElement{
     private Map<String, DataInfo> rootElement;
} 

public class DataInfo {
     private State state= null;
     private String type= null;
    // all your field under 1, 2

}

And your json should look like this

        "rootElement": {

          "1": {
                "state": {
                 "on": false,
                "bri": 0,
                "hue": 0,
                "sat": 0,
                "effect": "none",
                "xy": [
                    0,
                    0
                ],
                "ct": 0,
                "alert": "none",
                "colormode": "hs",
                "reachable": false
            },
            "type": "Extended color light",
            "name": "Hue Lamp 1",
            "modelid": "LCT001",
            "manufacturername": "Philips",
            "uniqueid": "00:17:88:01:00:f4:5a:aa-0b",
            "swversion": "5.23.1.13452"
        },

      // other object
}
Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
  • response().getRootElement().get(*).getName(). in this *, it said that need to fiil Object o. what is that ? – Tommy Wu Sep 26 '16 at 06:50
  • what i mean is when i'm trying to GET data from the API and add it into an arraylist, the code is like this right ? `List item = new ArrayList<>(); for (int i = 0; i < responsePhilipsLightses.size(); i++) { item.add(responsePhilipsLightses.get(i).getResponse().get(i).getName()); } ` – Tommy Wu Sep 26 '16 at 06:57
  • but between getResponse().get("*")‌​.getName(), in this * section, it said it need to fill the object, so what is this object – Tommy Wu Sep 26 '16 at 07:00
  • response object will be your RootElement class object , you can then iterate over it to get 1,2 .. – Jekin Kalariya Sep 26 '16 at 08:01
0

@tommy wu , try this

JSONObject questionMark = new JSONObject("your string containing whole json object");
    Iterator keys = questionMark.keys();

    while(keys.hasNext()) {
        // loop to get the dynamic key
        String currentDynamicKey = (String)keys.next();

        // get the value of the dynamic key
        JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey);

        // do something here with the value...
    }