Search code examples
javajsonphilips-hue

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. Is my code below wrong? Please help me. Thanks in advance

Json example below

{"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();
   }

Solution

  • I presume you want to to:

    jsonArray = jsonObject.getJSONObject("action").getJSONArray("xy");