0

I want to extract a value from JSON below using JSONObject. The value is inside dataand 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 ?

1
  • jsonMainArr[0][0]? Commented Feb 18, 2015 at 20:10

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@Frugo I'm glad you like it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.