B bluedude Well-Known Member Licensed User Longtime User Dec 17, 2010 #1 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
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 Dec 17, 2010 #2 Code: Copy { "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: Code: Copy For i = 0 To jsonItems.Size - 1 jsonItems = jsonItems.Get(i) '<---------- Log(jsonItems.Get("title")) Next To: Code: Copy For i = 0 To jsonItems.Size - 1 jsonresultMap = jsonItems.Get(i) Log(jsonResultMap.Get("title")) Next Upvote 0
Code: Copy { "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: Code: Copy For i = 0 To jsonItems.Size - 1 jsonItems = jsonItems.Get(i) '<---------- Log(jsonItems.Get("title")) Next To: Code: Copy For i = 0 To jsonItems.Size - 1 jsonresultMap = jsonItems.Get(i) Log(jsonResultMap.Get("title")) Next