Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't access property #38

Closed
douma opened this issue Aug 17, 2014 · 2 comments
Closed

Can't access property #38

douma opened this issue Aug 17, 2014 · 2 comments

Comments

@douma
Copy link

douma commented Aug 17, 2014

This is my json:
{"error":{"title":"Cannot login","message":"Username is required"}}

This is my code, the last println is never executed:
let json = JSONValue(response)
println(response)
if let userName = json["error"]["title"].string{
println(userName)
}

@douma
Copy link
Author

douma commented Aug 17, 2014

When running the code below:

    let json = JSONValue(response)
    let userName = json["error"]["title"]
    println(response)
    println(userName)

I receive this error:

{"error":{"title":"Cannot login","message":"Username is required"}}
JSON Keypath Error: Incorrect Keypath "/error/title"

@douma
Copy link
Author

douma commented Aug 17, 2014

Ok solved it with:

//make sure var response = json string with type NSData and parse this below

    let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(response, options: NSJSONReadingOptions.MutableContainers, error: nil)

    var json = JSONValue(jsonObject)
    println(json["error"]["title"].string)

@douma douma closed this as completed Aug 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant