0

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":{}}
7
  • can you show what you are trying to parsing? Commented Apr 6, 2016 at 16:33
  • lights is not a type jsonarray post your json string Commented Apr 6, 2016 at 16:34
  • 1
    lights is a JSONObject and not an array Commented Apr 6, 2016 at 16:35
  • Added the jsonString data. Lights is a JSONArray. Commented Apr 6, 2016 at 16:35
  • here lights is jsonobject you can't parse it to jsonarray Commented Apr 6, 2016 at 16:36

5 Answers 5

1

the key "lights" maps to a JSONObject and you are converting it to a JSONArray hence the exception.Change the line

lights = jsonObject.getJSONArray("lights");

to

JSONObject lights = jsonObject.getJSONObject("lights");
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that "lights" is not a JSONArray but a JSONObject. So to retrieve the contents of "lights" you need to iterate through all the objects of "lights" using the keys() method.

Replace the code from your try block with the following code to iterate through all the objects inside "lights".

 JSONObject jsonObject = new JSONObject(jsonString);
 JSONObject lights = jsonObject.getJSONObject("lights");
 Iterator<String> keyIterator = lights.keys();

 while (keys.hasNext()) {
     String key = keyIterator.next();
     JSONObject l = lights.getJSONObject(key);

     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");
 }

Comments

0

The following code should work for you:

String jsonString = serviceHandler.makeServiceCall(BASE_URL, ServiceHandler.GET);

if (jsonString != null) {
    try {
        JSONObject jsonObject = new JSONObject(jsonString);


        for (int 1 = 1; i <= 3; i++) {
            JSONObject l = jsonObject.getJSONObject("lights").getJSONObject(Integer.toString(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");
}

Comments

0

How to parse the JSON objects with each different key and value, using Java? visit this link.. and there you can access different key and values with that. so you don't have to worry about key. and you are having key like "1" and "2" and so on. so just iterate through lights object and you will get your answer. if you have any problem with that feel free to ask.

JSONObject json = new JSONObject("<jsonString>");
Iterator<String> keys = json.keys();

while (keys.hasNext()) {
    String key = keys.next();
    System.out.println("Key :" + key + "  Value :" + json.get(key));
}

from this you will get key and after that you have to just get values of that object.

Comments

0

In your JSONObject every key maps to a JSONObject not to a JSONArray. So you have to read like this from your JSONObject

JSONObject lights = jsonObject.getJSONObject("lights");

JSONObject schedules = jsonObject.getJSONObject("schedules");

JSONObject config = jsonObject.getJSONObject("config");

JSONObject groups = jsonObject.getJSONObject("groups");

JSONObject scenes = jsonObject.getJSONObject("scenes");

Hope it will help for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.