Json parsing problems

bluedude

Well-Known Member
Licensed User
Longtime User
I must be totallt stupid but below json does not work for me to parse:

{
"code": 200,
"data": {
"items": [
{
"title": "test 0"
},
{
"title": "test 1"
}
]
}
}

I use the following code to do this:

Dim JSON As JSONParser
Dim jsonMap As Map
Dim jsonResultMap As Map 'helper map for navigating
Dim jsonItems As List

'initialize json
JSON.Initialize(File.ReadString(File.DirAssets, "example1.json"))

'get objects
jsonMap = JSON.NextObject

'get data object for non array items
jsonResultMap=jsonMap.Get("data")
'move to next object from data structure

'or use a list for arrays
'jsonResultMap=jsonResultMap.Get("data")
jsonItems = jsonResultMap.Get("items")
For i = 0 To jsonItems.Size - 1
jsonItems = jsonItems.Get(i)
Log(jsonItems.Get("title"))
Next
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
{
    "code": 200,
    "data": 
    {
        "items": 
        [
            {
            "title": "test 0"
            },
            {
            "title": "test 1"
            }
        ]
    }
}
The "items" array contains two objects (each with a single "title" value).
You should change this:
B4X:
For i = 0 To jsonItems.Size - 1
        jsonItems = jsonItems.Get(i) '<----------
        Log(jsonItems.Get("title"))
    Next
To:
B4X:
For i = 0 To jsonItems.Size - 1
        jsonresultMap = jsonItems.Get(i)
        Log(jsonResultMap.Get("title"))
    Next
 
Upvote 0
Top