Xojo Plugin
Xojo Plugin
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 Xojo Plugin Downloads
Dim success As Boolean
success = False
Dim json As New 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.
Dim jsonStr As String
jsonStr = "{""employees"":[{""firstName"":""John"", ""lastName"":""Doe""},{""firstName"":""Anna"", ""lastName"":""Smith""},{""firstName"":""Peter"",""lastName"":""Jones""}]}"
success = json.Load(jsonStr)
If (success <> True) Then
System.DebugLog(json.LastErrorText)
Return
End If
// Get the "employees" array.
Dim employees As Chilkat.JsonArray
employees = json.ArrayOf("employees")
If (json.LastMethodSuccess = False) Then
System.DebugLog("employees member not found.")
Return
End If
// Iterate over each employee, getting the JSON object at each index.
Dim numEmployees As Int32
numEmployees = employees.Size
Dim i As Int32
i = 0
While i < numEmployees
Dim empObj As Chilkat.JsonObject
empObj = employees.ObjectAt(i)
System.DebugLog("employee[" + Str(i) + "] = " + empObj.StringOf("firstName") + " " + empObj.StringOf("lastName"))
i = i + 1
Wend