如何使用 java 获取 json 中的数组?
How to get array in json using java?
请帮助我如何获取 xy 的值我已经尝试了下面的代码但它不起作用。我下面的代码错了吗?请帮我。提前致谢
Json 下面的例子
{"name":"room ko","lights":["3","2"],"type":"Room","state":{"all_on":true,"any_on":true},"recycle":false,"class":"Living room","action":{"on":true,"bri":254,"hue":34704,"sat":198,"effect":"none","xy":[0.3228,0.3291],"ct":167,"alert":"none","colormode":"xy"}
这里是我目前试过的代码。
String message="";
JSONArray jsonArray = null;
try {
JSONObject jsonObject = new JSONObject(s);
message = jsonObject.getString("name");
jsonArray = jsonObject.getJSONArray("xy");
Log.d(TAG,jsonArray);//error in here
} catch (JSONException e) {
e.printStackTrace();
}
我猜你想:
jsonArray = jsonObject.getJSONObject("action").getJSONArray("xy");
请帮助我如何获取 xy 的值我已经尝试了下面的代码但它不起作用。我下面的代码错了吗?请帮我。提前致谢
Json 下面的例子
{"name":"room ko","lights":["3","2"],"type":"Room","state":{"all_on":true,"any_on":true},"recycle":false,"class":"Living room","action":{"on":true,"bri":254,"hue":34704,"sat":198,"effect":"none","xy":[0.3228,0.3291],"ct":167,"alert":"none","colormode":"xy"}
这里是我目前试过的代码。
String message="";
JSONArray jsonArray = null;
try {
JSONObject jsonObject = new JSONObject(s);
message = jsonObject.getString("name");
jsonArray = jsonObject.getJSONArray("xy");
Log.d(TAG,jsonArray);//error in here
} catch (JSONException e) {
e.printStackTrace();
}
我猜你想:
jsonArray = jsonObject.getJSONObject("action").getJSONArray("xy");