Android Question Unable to access List items within Map of JSON data

fikayo

Member
I have the following json structure:


{

"item1": "abc",

"item2": 123,

"item3": true,

"item4": ["a", "b","c"],

"item5": [

[1,2,3],

["x","y","z"],

[

{"five20":["i","j","k"]},

{"five21":"opq"}

]

]

}

I have no problems (yet) with items 1 to 4 which have primitive data types.

Item5 is a List of 3 list items (multidimentional array) of different types of variables (Int, String, Map respectively)

I tried to access the 3rd element of item5 as follows:


B4X:
Dim JSON As JSONParser
Dim Map1 As Map JSON.Initialize(File.ReadString(File.DirAssets, "myfile.json"))
 Map1 = JSON.NextObject
'
'
 Dim strJ As String
strJ = Map1.Get("item5").As(List).Get(2).As(List).Get(0).As(Map).Get("five20").As(List).Get(1) ' should give me "j"

But I got the error:
java.lang.IndexOutOfBoundsExecption: Index: 2, Size: 2

Debug at breakpoint also shows that only the first two elements of item5 are recognized.

How can I access the third element which contains Maps?
 

toby

Well-Known Member
Licensed User
Longtime User
It works for me without any change.

log data:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
j
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
If the JSON structure is variable and contains a mixture of lists and maps.
you must validate the type of the object that contains the elements and subelements.
In addition to verifying if the element you are looking for exists.

simple example:
B4X:
    Dim JsonText As String
    JsonText = $"{
          "item1": "abc",
          "item2": 123,
          "item3": true,
          "item4": [
            "a",
            "b",
            "c"
          ],
          "item5": [
            [
              1,
              2,
              3
            ],
            [
              "x",
              "y",
              "z"
            ],
            [
              {
                "cinco20": [
                  "i",
                  "j",
                  "k"
                ]
              },
              {
                "cinco21": " opq"
              }
            ]
          ]
        }
        "$   
    Log(JsonText.As(JSON).ToMap.Get("item5").As(List).Get(0).As(List).Get(0))
    Log(JsonText.As(JSON).ToMap.Get("item5").As(List).Get(2).As(List).Get(0).As(Map).Get("cinco20").As(List).Get(0))
    Log(JsonText.As(JSON).ToMap.Get("item5").As(List).Get(2).As(List).Get(1).As(Map).Get("cinco21"))
result:
 
Upvote 0

pliroforikos

Active Member
Licensed User
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As Map = parser.NextObject
Dim item2 As Int = jRoot.Get("item2")
Dim item1 As String = jRoot.Get("item1")
Dim item4 As List = jRoot.Get("item4")
For Each colitem4 As String In item4
Next
Dim item3 As String = jRoot.Get("item3")
Dim item5 As List = jRoot.Get("item5")
For Each colitem5 As List In item5
 For Each colcolitem5 As Int In colitem5
 Next
Next

This is a magnificent tools to parse json
 
Upvote 0

fikayo

Member
After reading the response #2 by Toby, I looked again at all my code and app structure and it took me more than a whole day to see a simple error glaring at me - my code was pointing at a different json file from the one I was seeing on the screen! A very embarrassing mistake. So it turned out that there was no problem at all.
I thank you all for your help and apologise for taking your time unnecessarily.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…