(Swift) Load a JSON Array
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.
func chilkatTest() {
var success: Bool
// 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("{\"array\":")
sbJson.append(jsonArrayStr)
sbJson.append("}")
let json = CkoJsonObject()!
json.load(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
}
|