Json parsing problems

Discussion in 'Questions' started by bluedude, Dec 17, 2010.

  1. bluedude

    bluedude Well-Known Member Licensed 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
  2. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    {
        
    "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:
    For i = 0 To jsonItems.Size - 1
            jsonItems = jsonItems.Get(i) 
    '<----------
            Log(jsonItems.Get("title"))
        
    Next
    To:
    Code:
    For i = 0 To jsonItems.Size - 1
            jsonresultMap = jsonItems.Get(i)
            
    Log(jsonResultMap.Get("title"))
        
    Next

Share This Page

Loading...