如何使用Java进入JSON中的数组



请帮助我如何获得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");

最新更新