Swift
Swift
JSON Copy Objects
See more JSON Examples
Copy objects from one JSON document to another.Chilkat Swift Downloads
func chilkatTest() {
let json1 = CkoJsonObject()!
json1.updateString(jsonPath: "ID1.cn", value: "Name")
json1.updateString(jsonPath: "ID1.objectGUID", value: "GUID")
json1.updateString(jsonPath: "ID2.cn", value: "Name")
json1.updateString(jsonPath: "ID2.objectGUID", value: "GUID")
json1.emitCompact = false
print("\(json1.emit()!)")
// json1 contains:
// {
// "ID1": {
// "cn": "Name",
// "objectGUID": "GUID"
// },
// "ID2": {
// "cn": "Name",
// "objectGUID": "GUID"
// }
// }
let json2 = CkoJsonObject()!
json2.updateString(jsonPath: "Name1.ID1.cn", value: "Name")
json2.updateString(jsonPath: "Name1.ID1.objectGUID", value: "GUID")
json2.updateString(jsonPath: "Name1.ID2.cn", value: "Name")
json2.updateString(jsonPath: "Name1.ID2.objectGUID", value: "GUID")
json2.updateString(jsonPath: "Name2.ID3.cn", value: "Name")
json2.updateString(jsonPath: "Name2.ID3.objectGUID", value: "GUID")
json2.emitCompact = false
print("\(json2.emit()!)")
// {
// "Name1": {
// "ID1": {
// "cn": "Name",
// "objectGUID": "GUID"
// },
// "ID2": {
// "cn": "Name",
// "objectGUID": "GUID"
// }
// },
// "Name2": {
// "ID3": {
// "cn": "Name",
// "objectGUID": "GUID"
// }
// }
// }
// Copy Name1, Name2 into json1
var i: Int = 0
var numMembers: Int = json2.size.intValue
while i < numMembers {
var jsonObj: CkoJsonObject? = json2.object(at: i)
json1.appendObjectCopy(name: json2.name(at: i), jsonObj: jsonObj)
i = i + 1
}
// Now see what json1 contains:
print("\(json1.emit()!)")
// {
// "ID1": {
// "cn": "Name",
// "objectGUID": "GUID"
// },
// "ID2": {
// "cn": "Name",
// "objectGUID": "GUID"
// },
// "Name1": {
// "ID1": {
// "cn": "Name",
// "objectGUID": "GUID"
// },
// "ID2": {
// "cn": "Name",
// "objectGUID": "GUID"
// }
// },
// "Name2": {
// "ID3": {
// "cn": "Name",
// "objectGUID": "GUID"
// }
// }
// }
}