Sample code for 30+ languages & platforms
Swift

Using JSON StringOf with Non-String Members

See more JSON Examples

If a JSON member is a boolean, integer, or null, using StringOf will return its string representation.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    // Create JSON with members of different data types.
    let json = CkoJsonObject()!

    json.updateInt(jsonPath: "a", value: 123)
    json.updateBool(jsonPath: "b", value: true)
    json.updateNull(jsonPath: "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")

}