{ "first": "John", "last": "Smith", "age": 35, "employeed": true, "hobbies": [ "fishing", "bowling", "programming" ], "relatives": { "mother": "Susan", "father": "Eric", "siblings": null } }
Here is a simple mapping JSON types to Swift types(JSON –> Swift)
null ==> nil
Number ==> Int, Float, Double
String ==> Bool
Array ==> [AnyObject]
Object ==> [String:AnyObject]
guard let arrayOfPhotoDictionaries = photosDictionary["photo"] as? [[String:AnyObject]] else { print("Cannot find key 'photo' in \(photosDictionary)") return }
for (index, photo) in arrayOfPhotoDictionaries.enumerated(){ print("\(index):\(photo)") }
let rarities = ["Free", "Common"] for rarity in rarities { numCostForRarityItemsDictionary[rarity] = 0 sumCostForRarityDictionary[rarity] = 0 } guard let rarityForCard = cardDictionary["rarity"] as? String else { print("Cannot find key 'rarityForCard' in \(cardDictionary)") return } numCostForRarityItemsDictionary[rarityForCard]! += 1 sumCostForRarityDictionary[rarityForCard]! += manaCost guard let arrayOfBasicSetCardDictionaries = parsedHearthstoneJSON["Basic"] as? [[String:AnyObject]] else { print("cannot find key 'basic" in \(parsedHearthstoneJSON)") return }