1

How can I retrieve "B2" from the following? JSONArray?

        JSONArray o = new JSONArray();
    for(int i = 0; i < 3; i++) {
        List<Object> list = new ArrayList<Object>();
        list.add("B" + i);
        list.add(100+i);
        o.put(list);
    }

I'm thinking it's something like the following, although this isn't at all correct.

o.get(2)[0]
3
  • No, it would be o.getInt(2). See json.org/javadoc/org/json/JSONArray.html#getInt%28int%29 Commented Mar 5, 2013 at 23:43
  • the second entry isn't an integer? it was a list of size 2. Commented Mar 5, 2013 at 23:48
  • 1
    o.getJSONArray(2).getString(0) Commented Mar 5, 2013 at 23:53

2 Answers 2

1

The problem is that you're using put(), which:

Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.

So, this will add an array into your array. Something like this:

[
  [
    "B1", "B2"
  ]
]

If this was intended, then you'll first need to get the inner array and then it's second entry:

o.getJSONArray(0).getInt(1);

Otherwise, if you want to fill your existing JSONArray o with the entries from the List, you'll want to use the JSONArray(Collection)-constructor.

Sign up to request clarification or add additional context in comments.

Comments

0

You've created an array of bidimensional arrays. The following code snippet should work:

o.getJSONArray(2).getString(0);

Comments

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.