Colin Douglas
Colin Douglas

Reputation: 583

JSONArray within JSONArray

I'm currently confused why I cannot pull a JSONArray from a JSONArray in my android application. An example and a snippet of my source code are given below.

//The JSON

   {
     "currentPage":1,
     "data":[
        {
          "id":"dimtrs",
          "name":"Bud Light",
          "breweries":[
              {
                "id":"BznahA",
                "name":"Anheuser-Busch InBev",
              }
           ]
        }
    ],
    "status":"success"
  }

Now I'm trying to retreive the "breweries" array.

//Code Snippet

...

JSONObject jsonobject = new JSONObject(inputLine);

JSONArray jArray = jsonobject.getJSONArray("data");

JSONArray jsArray = jArray.getJSONArray("breweries");

...

I can pull objects out of the data array just fine, but I cannot get the "breweries" array from the "data" array using my current code.

The error for jsArray is:

The method .getJSONArray(int) in the type JSONArray is not applicable for the arguments String

So what is the correct way to pull the "breweries" array out of the "data" array?

Thanks for the help in advance!

Upvotes: 4

Views: 504

Answers (1)

Charles Goodwin
Charles Goodwin

Reputation: 6642

It is because "breweries" is in the 1st object of the "data" array, not directly on the array itself. You are trying to get a key from an array.

So you want to call jArray.getJSONObject(0).getJSONArray("breweries"); or something to that effect.

As Sambhav Sharma explains in a comment, the reason for the error is that get methods for JSONArray expect an int and not a String as their argument.

Upvotes: 4

Related Questions