Sample code for 30+ languages & platforms
Xbase++

JSON: Nested Objects

See more JSON Examples

Here we have a JSON object that contains nested JSON objects. This example demonstrates how to access the contents of the nested objects.
{
  "name": "donut",
  "image":
    {
    "fname": "donut.jpg",
    "w": 200,
    "h": 200
    },
  "thumbnail":
    {
    "fname": "donutThumb.jpg",
    "w": 32,
    "h": 32
    }
}

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJson
LOCAL cJsonStr
LOCAL oImageObj
LOCAL oThumbObj

nSuccess := 0

oJson := CreateObject("Chilkat.JsonObject")

//  This is the above JSON with whitespace chars removed (SPACE, TAB, CR, and LF chars).
//  The presence of whitespace chars for pretty-printing makes no difference to the Load
//  method. 
cJsonStr := '{"name": "donut","image":{"fname": "donut.jpg","w": 200,"h": 200},"thumbnail":{"fname": "donutThumb.jpg","w": 32,"h": 32}}'

nSuccess := oJson:Load(cJsonStr)
IF (nSuccess == 0)
    ? oJson:LastErrorText
    oJson:destroy()
    RETURN
ENDIF

//  Get the "image" object.
oImageObj := CreateObject("Chilkat.JsonObject")
oJson:ObjectOf2("image", oImageObj)

? "image: fname=" + oImageObj:StringOf("fname") + ", width=" + Str(oImageObj:IntOf("w")) + ", height=" + Str(oImageObj:IntOf("h"))

//  Get the "thumbnail" object.
oThumbObj := CreateObject("Chilkat.JsonObject")
oJson:ObjectOf2("thumbnail", oThumbObj)

? "thumbnail: fname=" + oThumbObj:StringOf("fname") + ", width=" + Str(oThumbObj:IntOf("w")) + ", height=" + Str(oThumbObj:IntOf("h"))

oJson:destroy()
oImageObj:destroy()
oThumbObj:destroy()