Swift
Swift
JSON Paths
See more JSON Examples
Demonstrates using "Chilkat JSON Paths" to access parts of a JSON document, or to iterate over parts.This example uses the following JSON document:
{
"nestedArray" : [
[
[1,2,3],
[4,5,6],
[7,8,9,10]
],
[
[11,12,13],
[14,15,16],
[17,18,19,20]
],
[
[21,22,23],
[24,25,26],
[27,28,29,30],
[31,32,33,34,35,36]
]
],
"nestedObject" : {
"aaa" : {
"bb1" : {
"cc1" : "c1Value",
"cc2" : "c2Value",
"cc3" : "c3Value"
},
"bb2" : {
"dd1" : "d1Value",
"dd2" : "d2Value",
"dd3" : "d3Value"
}
}
},
"mixture" : {
"arrayA" : [
{ "fruit": "apple", "animal": "horse", "job": "fireman", "colors": ["red","blue","green"] },
{ "fruit": "pear", "animal": "plankton", "job": "waiter", "colors": ["yellow","orange","purple"] },
{ "fruit": "kiwi", "animal": "echidna", "job": "astronaut", "colors": ["magenta","tan","pink"] }
]
},
"name.with.dots" : { "grain" : "oats" }
}
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
let json = CkoJsonObject()!
json.emitCompact = false
// Assume the file contains the data as shown above..
success = json.loadFile(path: "qa_data/json/pathSample.json")
if success == false {
print("\(json.lastErrorText!)")
return
}
// First, let's get the value of "cc1"
// The path to this value is: nestedObject.aaa.bb1.cc1
print("\(json.string(of: "nestedObject.aaa.bb1.cc1")!)")
// Now let's get number 18 from the nestedArray.
// It is located at nestedArray[1][2][1]
// (remember: Indexing is 0-based)
print("This should be 18: \(json.int(of: "nestedArray[1][2][1]").intValue)")
// We can do the same thing in a more roundabout way using the
// I, J, and K properties. (The I,J,K properties will be convenient
// for iterating over arrays, as we'll see later.)
json.i = 1
json.j = 2
json.k = 1
print("This should be 18: \(json.int(of: "nestedArray[i][j][k]").intValue)")
// Let's iterate over the array containing the numbers 17, 18, 19, 20.
// First, use the SizeOfArray method to get the array size:
var sz: Int = json.size(ofArray: "nestedArray[1][2]").intValue
// The size should be 4.
print("size of array = \(sz) (should equal 4)")
// Now iterate...
var i: Int
for i = 0; i <= sz - 1; i++ {
json.i = i
print("\(json.int(of: "nestedArray[1][2][i]").intValue)")
}
// Let's use a triple-nested loop to iterate over the nestedArray:
var j: Int
var k: Int
// szI should equal 1.
var szI: Int = json.size(ofArray: "nestedArray").intValue
for i = 0; i <= szI - 1; i++ {
json.i = i
var szJ: Int = json.size(ofArray: "nestedArray[i]").intValue
for j = 0; j <= szJ - 1; j++ {
json.j = j
var szK: Int = json.size(ofArray: "nestedArray[i][j]").intValue
for k = 0; k <= szK - 1; k++ {
json.k = k
print("\(json.int(of: "nestedArray[i][j][k]").intValue)")
}
}
}
// Now let's examine how to navigate to JSON objects contained within JSON arrays.
// This line of code gets the value "kiwi" contained within "mixture"
print("\(json.string(of: "mixture.arrayA[2].fruit")!)")
// This line of code gets the color "yellow"
print("\(json.string(of: "mixture.arrayA[1].colors[0]")!)")
// Getting an object at a path:
// This gets the 2nd object in "arrayA"
let obj2 = CkoJsonObject()!
json.objectOf2(jsonPath: "mixture.arrayA[1]", jsonObj: obj2)
// This object's "animal" should be "plankton"
print("\(obj2.string(of: "animal")!)")
// Note that paths are relative to the object, not the absolute root of the JSON document.
// Starting from obj2, "purple" is at "colors[2]"
print("\(obj2.string(of: "colors[2]")!)")
// Getting an array at a path:
// This gets the array containing the colors red, green, blue:
let arr1 = CkoJsonArray()!
json.arrayOf2(jsonPath: "mixture.arrayA[0].colors", jarr: arr1)
var szArr1: Int = arr1.size.intValue
for i = 0; i <= szArr1 - 1; i++ {
print("\(i): \(arr1.string(at: i)!)")
}
// The Chilkat JSON path uses ".", "[", and "]" chars for separators. When a name
// contains one of these chars, use double-quotes in the path:
print("\(json.string(of: "\"name.with.dots\".grain")!)")
}