Xbase++
Xbase++
JSON: Nested Array
See more JSON Examples
Here we have a JSON object that contains nested arrays. This example demonstrates how to access the contents of the nested arrays.
{
"numbers" : [
["even", 2, 4, 6, 8],
["prime", 2, 3, 5, 7, 11, 13]
] }
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oJson
LOCAL cJsonStr
LOCAL oOuterArray
LOCAL nNumArrays
LOCAL i
LOCAL oInnerArray
LOCAL nNumInnerItems
LOCAL j
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 := '{ "numbers" : [ ["even", 2, 4, 6, 8], ["prime", 2, 3, 5, 7, 11, 13] ] }'
nSuccess := oJson:Load(cJsonStr)
IF (nSuccess != 1)
? oJson:LastErrorText
oJson:destroy()
RETURN
ENDIF
// Get the value of the "numbers" object, which is an array that contains JSON arrays.
oOuterArray := oJson:ArrayOf("numbers")
IF (oJson:LastMethodSuccess == 0)
? "numbers array not found."
oJson:destroy()
RETURN
ENDIF
nNumArrays := oOuterArray:Size
FOR i := 0 TO nNumArrays - 1
oInnerArray := oOuterArray:ArrayAt(i)
// The first item in the innerArray is a string
? oInnerArray:StringAt(0) + ":"
nNumInnerItems := oInnerArray:Size
FOR j := 1 TO nNumInnerItems - 1
? " " + Str(oInnerArray:IntAt(j))
NEXT
oInnerArray:destroy()
NEXT
oOuterArray:destroy()
oJson:destroy()