gpt4 book ai didi

java - Java 中的字符串到 JSONArray

转载 作者:行者123 更新时间:2023-12-01 10:06:07 26 4
gpt4 key购买 nike

我正在尝试将来自 url(本例中为 Philips Hue 模拟器)的 Json 结果解析为 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");
}

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

type org.json.JSONObject cannot be converted to 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":{}}

最佳答案

键“lights”映射到 JSONObject 并且您将其转换为 JSONArray 因此出现异常。更改行

lights = jsonObject.getJSONArray("lights");

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

关于java - Java 中的字符串到 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36457064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com