简体   繁体   English

如何使用Java在JSON中获取数组?

[英]How to get array in json using java?

Please help me how to get the value of xy i have tried the code below but its not working. 请帮助我如何获取xy的值,我已经尝试过以下代码,但无法正常工作。 Is my code below wrong? 我的下面的代码错误吗? Please help me. 请帮我。 Thanks in advance 提前致谢

Json example below 下面的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"}

Here the code I have tried so far. 到目前为止,我在这里尝试过的代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM