Xbase++
Xbase++
JSON: Array of Objects
See more JSON Examples
Here we have a JSON object that contains an array, where each element in the array is a JSON object. This example demonstrates how to access the objects contained within an array.
{
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]
}
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oJson
LOCAL cJsonStr
LOCAL oEmployees
LOCAL nNumEmployees
LOCAL i
LOCAL oEmpObj
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 := '{"employees":[{"firstName":"John", "lastName":"Doe"},{"firstName":"Anna", "lastName":"Smith"},{"firstName":"Peter","lastName":"Jones"}]}'
nSuccess := oJson:Load(cJsonStr)
IF (nSuccess != 1)
? oJson:LastErrorText
oJson:destroy()
RETURN
ENDIF
// Get the "employees" array.
oEmployees := oJson:ArrayOf("employees")
IF (oJson:LastMethodSuccess == 0)
? "employees member not found."
oJson:destroy()
RETURN
ENDIF
// Iterate over each employee, getting the JSON object at each index.
nNumEmployees := oEmployees:Size
i := 0
DO WHILE i < nNumEmployees
oEmpObj := oEmployees:ObjectAt(i)
? "employee[" + Str(i) + "] = " + oEmpObj:StringOf("firstName") + " " + oEmpObj:StringOf("lastName")
oEmpObj:destroy()
i := i + 1
ENDDO
oEmployees:destroy()
oJson:destroy()