1

from this link I would like to get the commonName

I tried this but it didn't work?!

let commonName = object["toLocationDisambiguation"][0]["disambiguationOptions"][1]["place"][2]["commonName"].stringValue
1

1 Answer 1

1

Option 1 (Normal)

if let toLocationDisambiguation = object["toLocationDisambiguation"] as? Dictionary<String, AnyObject> {
    if let disambiguationOptions = toLocationDisambiguation["disambiguationOptions"] as? Array<AnyObject> {
        if let first = disambiguationOptions.first as? [String: AnyObject] {
            if let place = first["place"] as? [String: AnyObject] {
                let commonName = place["commonName"] as! String
                print("Common Name: ", commonName)
            }
        }
    }
}

Option 2 (Type Aliases)

typealias MyDictionary = [String: AnyObject]
typealias MyArray = [MyDictionary]

if let toLocationDisambiguation = object["toLocationDisambiguation"] as? MyDictionary {
    if let disambiguationOptions = toLocationDisambiguation["disambiguationOptions"] as? MyArray {
        if let first = disambiguationOptions.first {
            if let place = first["place"] as? MyDictionary {
                let commonName = place["commonName"] as! String
                print("Common Name: ", commonName)
            }
        }
    }
}

Option 3 (SwiftyJSON for Objective-C like syntax)

Take a look at SwiftyJSON.

let object = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! Dictionary<String, AnyObject>
let json = JSON(object)
let commonName = json["toLocationDisambiguation"]["disambiguationOptions"][0]["place"]["commonName"].stringValue
print("Common Name: ", commonName)
Sign up to request clarification or add additional context in comments.

5 Comments

I am using SwiftyJSON so I tried what you suggested: if let disambiguations = object["toLocationDisambiguation"] as? MyArray { if let commonName = disambiguations.first as? MyDictionary { let commonName = object["commonName"].stringValue } } now I get the error: downcast form 'MyDictionay?' to 'MyDictionary'
I fixed my code. Basically, get rid of the as? MyDictionary as it's unnecessary.
thanks but I'm really stuck, not sure how I'm supposed to use your syntax for (_,object):(String, JSON) in readableJSON { typealias MyDictionary = [String: AnyObject] typealias MyArray = [MyDictionary] if let disambiguations = object["toLocationDisambiguation"] as? MyArray { if let first = disambiguations.first { let commonName = disambiguations NSLog("Dis: \(disambiguations)") } } commonNameArray.append(commonName) NumberOfRows = commonNameArray.count
Sorry if I confused things, I fixed my answer now.
thanks, the SwiftyJSON one worked although it's not looping in my for loop let jsonData = NSData(contentsOfURL: url!) let readableJSON = JSON(data: jsonData!, options: NSJSONReadingOptions.MutableContainers, error: nil) let object = try! NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.AllowFragments) as! Dictionary<String, AnyObject> let json = JSON(object) for (_,object):(String, JSON) in readableJSON { let commonName = json["toLocationDisambiguation"]["disambiguationOptions"][0]["place"]["commonName"].stringValue commonNameArray.append(commonName)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.