Java中{JARArray的字符串

时间:2016-04-06 16:31:57

标签: java android json

我正在尝试将URL中的Json结果(在本例中为Philips Hue模拟器)解析为Android Studio中的JsonArray。到目前为止,我有以下代码:

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

但是,当我尝试运行此代码时,我得到一个(...)

  

类型org.json.JSONObject无法转换为JSONArray

错误。我已经尝试了一些其他方法将字符串转换为JsonArray或循环字符串,但没有成功。

我做错了什么?

编辑: 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":{}}

5 个答案:

答案 0 :(得分:1)

键“灯”映射到JSONObject,您将其转换为JSONArray因此异常。更改行

lights = jsonObject.getJSONArray("lights");

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

答案 1 :(得分:0)

How to parse the JSON objects with each different key and value, using Java? 访问此链接..在那里,您可以访问不同的键和值。所以你不必担心钥匙。而且你有像#34; 1&#34;和&#34; 2&#34;等等。所以只需遍历灯光对象,您就会得到答案。如果您有任何问题,请随时提出。

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));
}
从这里你将得到密钥,然后你必须得到该对象的值。

答案 2 :(得分:0)

以下代码适合您:

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

答案 3 :(得分:0)

问题是"lights"不是JSONArray而是JSONObject。因此,要检索"lights"的内容,您需要使用"lights"方法遍历keys()的所有对象。

使用以下代码替换try块中的代码,以遍历"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");
 }

答案 4 :(得分:0)

JSONObject的每个键中,映射到JSONArray而不是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");

中读到这样的内容
with some data

希望它对你有所帮助。