(Swift) Using JSON StringOf with Non-String Members
If a JSON member is a boolean, integer, or null, using StringOf will return its string representation.
func chilkatTest() {
// Create JSON with members of different data types.
let json = CkoJsonObject()!
json.updateInt("a", value: 123)
json.updateBool("b", value: true)
json.updateNull("c")
json.emitCompact = false
print("\(json.emit()!)")
// Resulting JSON:
// {
// "a": 123,
// "b": true,
// "c": null
// }
var a: String? = json.string(of: "a")
print("\(a!)")
var b: String? = json.string(of: "b")
print("\(b!)")
var c: String? = json.string(of: "c")
print("\(c!)")
// Output
// 123
// true
// null
// If you want to get the integer, boolean, or null value
// you need to use the methods matching the data type
var ival: Int = json.int(of: "a").intValue
var bval: Bool = json.bool(of: "b")
var hasNull: Bool = json.isNull(of: "c")
}
|