繁体   English   中英

如何使用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");

暂无
暂无

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

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