简体   繁体   中英

extracting value from JSON array

I want to extract a value from JSON below using JSONObject. The value is inside data and in this case is 3:

{
  "columns": [
    "count(n)"
  ],
  "data": [
    [
      3
    ]
  ]
}

I tried JSONArray jsonMainArr = jsonRes.getJSONArray("data"); and it's displaying [[3]] , how to extract that number 3 ?

data element holds nested array.

Since you already have this array in

JSONArray jsonMainArr = jsonRes.getJSONArray("data");

you can get its first (and only) inner array [3] with

jsonMainArr.getJSONArray(0)

Now you need to get its only element. You can do it with get(0) , or to get more precise return type getInt(0) .

This should work fine for you

int value = jsonRes.getJSONArray("data").getJSONArray(0).getInt(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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