Swift
Swift
Load a JSON Array
See more JSON Examples
The Chilkat JSON API requires the top-level JSON to be an object. Therefore, to load an array requires that it first be wrapped as an object.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Imagine we want to load this JSON array for parsing:
var jsonArrayStr: String? = "[{\"id\":200},{\"id\":196}]"
// First wrap it in a JSON object by prepending "{ "array":" and appending "}"
let sbJson = CkoStringBuilder()!
sbJson.append(value: "{\"array\":")
sbJson.append(value: jsonArrayStr)
sbJson.append(value: "}")
let json = CkoJsonObject()!
json.load(json: sbJson.getAsString())
// Now we can get the JSON array
var jArray: CkoJsonArray? = json.array(at: 0)
// Do what you want with the JSON array...
// For example:
var jObjId: CkoJsonObject? = jArray!.object(at: 0)
print("\(jObjId!.int(of: "id").intValue)")
jObjId = nil
jArray = nil
}