Android JSONArrays inside JSONArray

Here is the relevant part of my json file:

"categories": [
     [
         "Belgian Restaurant", 
         "belgian"
     ], 
     [
         "Brasserie", 
         "brasseries"
     ]
 ], 

What I want to do is get information from the second JSONArray(say, for example, "brasseries").

The following code worked to extract information from the first array:

JSONArray categories = jsonObject.getJSONArray("categories");
restaurant.setCat1(categories.getJSONArray(0).getString(1));

The result of this was “Belgian”, as expected, but then I tried the same for the second array:

restaurant.setCat2(categories.getJSONArray(1).getString(1));

and what Index 1 out of range [0..1)JSONException chose

I do not get it, since indexes 0 and 1 explicitly exist in the file ... Why does 0 work, not 1?

+4
source share
1 answer

, , :

restaurant.setCat2(categories.getJSONArray(0).getString(1));

JSON, , ,

as JSON KEY's , ,

, ( ).

JSON -,

{
  "categories": [
    {
      "id": "00",
      "name": "Some_name_0"
    },
    {
      "id": "01",
      "name": "Some_name_1"
    },
    {
      "id": "02",
      "name": "Some_name_2"
    },
    {
      "id": "03",
      "name": "Some_name_3"
    },
    {
      "id": "04",
      "name": "Some_name_4"
    }
  ]
}
+2

Source: https://habr.com/ru/post/1626875/


All Articles