I'm trying to parse a Json result from a url (the Philips Hue emulator in this case) into a JsonArray in Android Studio. I have the following code so far:
String jsonString = serviceHandler.makeServiceCall(BASE_URL, ServiceHandler.GET);
JSONArray lights;
if (jsonString != null) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
lights = jsonObject.getJSONArray("lights");
for (int i = 0; i < lights.length(); i++) {
JSONObject l = lights.getJSONObject(i);
String name = l.getString("name");
String bri = l.getString("bri");
String hue = l.getString("hue");
String sat = l.getString("sat");
String status = l.getString("on");
}
} catch (JSONException ex) {
ex.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Cannot get data from the url");
}
But, when I try to run this code I get a (...)
type org.json.JSONObject cannot be converted to JSONArray
error. I have tried some other ways to convert the string into a JsonArray or to loop through the string, but without success.
What I'm doing wrong??
Edit: The data in jsonString:
{"lights":{"1":{"state":{"on":false,"bri":254,"hue":4444,"sat":254,"xy":[0.0,0.0],"ct":0,"alert":"none","effect":"none","colormode":"hs","reachable":true},"type":"Extended color light","name":"Hue Lamp 1","modelid":"LCT001","swversion":"65003148","uniqueid":"00:17:88:01:00:d4:12:08-0a","pointsymbol":{"1":"none","2":"none","3":"none","4":"none","5":"none","6":"none","7":"none","8":"none"}},"2":{"state":{"on":false,"bri":254,"hue":23536,"sat":144,"xy":[0.346,0.3568],"ct":201,"alert":"none","effect":"none","colormode":"hs","reachable":true},"type":"Extended color light","name":"Hue Lamp 2","modelid":"LCT001","swversion":"65003148","uniqueid":"00:17:88:01:00:d4:12:08-0b","pointsymbol":{"1":"none","2":"none","3":"none","4":"none","5":"none","6":"none","7":"none","8":"none"}},"3":{"state":{"on":true,"bri":254,"hue":65136,"sat":254,"xy":[0.346,0.3568],"ct":201,"alert":"none","effect":"none","colormode":"hs","reachable":true},"type":"Extended color light","name":"Hue Lamp 3","modelid":"LCT001","swversion":"65003148","uniqueid":"00:17:88:01:00:d4:12:08-0c","pointsymbol":{"1":"none","2":"none","3":"none","4":"none","5":"none","6":"none","7":"none","8":"none"}}},"schedules":{"1":{"time":"2012-10-29T12:00:00","description":"","name":"schedule","command":{"body":{"scene":null,"on":true,"xy":null,"bri":null,"transitiontime":null},"address":"/api/newdeveloper/groups/0/action","method":"PUT"}}},"config":{"portalservices":false,"gateway":"192.168.2.1","mac":"00:00:88:00:bb:ee","swversion":"01005215","linkbutton":false,"ipaddress":"192.168.178.31:5510","proxyport":0,"swupdate":{"text":"","notify":false,"updatestate":0,"url":""},"netmask":"255.255.255.0","name":"Philips hue","dhcp":true,"proxyaddress":"","whitelist":{"newdeveloper":{"name":"test user","last use date":"2016-04-06T16:51:26","create date":"2012-10-29T12:00:00"}},"UTC":"2012-10-29T12:05:00"},"groups":{"1":{"name":"Group 1","action":{"on":true,"bri":254,"hue":33536,"sat":144,"xy":[0.346,0.3568],"ct":201,"alert":null,"effect":"none","colormode":"xy","reachable":null},"lights":["1","2"]}},"scenes":{}}
lightsis not a type jsonarray post your json string