This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func searchJSON(json: [String:Any], searchString: String) -> [String] { | |
var array: [String] = [] | |
let jsonKeys = json.keys | |
for i in 0..<jsonKeys.count { | |
let level1 = json[jsonKeys.index(jsonKeys.startIndex, offsetBy: i)] | |
if let level2 = json[level1.key] as? [String:Any] { | |
array.append(contentsOf: searchJSON(json: level2, searchString: searchString)) | |
} | |
else if let level2 = json[level1.key] as? [[String:Any]] { | |
for i in 0..<level2.count { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func printJSON(json: [String:Any]) { | |
let jsonKeys = json.keys //Gets the list of keys on the outer-most layer of the JSON | |
for i in 0..<jsonKeys.count { | |
let level1 = json[jsonKeys.index(jsonKeys.startIndex, offsetBy: i)] //retrieves the object with the specific keys | |
if let level2 = json[level1.key] as? [String:Any]{ //if the key is another object | |
printJSON(json: level2) //send it as a new json object to the function again | |
} else if let level2 = json[level1.key] as? [[String:Any]] { //if the key is an array of objects | |
for i in 0..<level2.count { //loop through the array | |
printJSON(json: level2[i]) //send each array element to the function | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let query = json["query"] as? [String:Any] { | |
if let pages = query["pages"] as? [String:Any] { | |
if let page = pages["30984"] as? [String:Any] { | |
if let revisions = page["revisions"] as? [[String:Any]] { | |
if let element = revisions[0] as? [String:Any] { | |
if let htmlData = element["*"] as? String { | |
print(htmlData) | |
} | |
} | |
} |