Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Json
string ls_JsonStr
oleobject loo_ImageObj
oleobject loo_ThumbObj

li_Success = 0

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Json
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// 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. 
ls_JsonStr = "{~"name~": ~"donut~",~"image~":{~"fname~": ~"donut.jpg~",~"w~": 200,~"h~": 200},~"thumbnail~":{~"fname~": ~"donutThumb.jpg~",~"w~": 32,~"h~": 32}}"

li_Success = loo_Json.Load(ls_JsonStr)
if li_Success = 0 then
    Write-Debug loo_Json.LastErrorText
    destroy loo_Json
    return
end if

// Get the "image" object.
loo_ImageObj = create oleobject
li_rc = loo_ImageObj.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.ObjectOf2("image",loo_ImageObj)

Write-Debug "image: fname=" + loo_ImageObj.StringOf("fname") + ", width=" + string(loo_ImageObj.IntOf("w")) + ", height=" + string(loo_ImageObj.IntOf("h"))

// Get the "thumbnail" object.
loo_ThumbObj = create oleobject
li_rc = loo_ThumbObj.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.ObjectOf2("thumbnail",loo_ThumbObj)

Write-Debug "thumbnail: fname=" + loo_ThumbObj.StringOf("fname") + ", width=" + string(loo_ThumbObj.IntOf("w")) + ", height=" + string(loo_ThumbObj.IntOf("h"))


destroy loo_Json
destroy loo_ImageObj
destroy loo_ThumbObj