How to access nested JSON in brackets in Swift?

前端 未结 2 1994
無奈伤痛
無奈伤痛 2021-01-26 04:52

I have a JSON response which is in brackets and I struggle to access the inner fields e.g. display_name with Swift. How can I do that?

Optional([\"result\": {
us         


        
相关标签:
2条回答
  • 2021-01-26 05:38

    In response result is dictionary not array, try to get like this

    let result = response!["result"]  as? [String:AnyObject]
    
    0 讨论(0)
  • 2021-01-26 05:39

    1-Get the result object from response
    2-Get the user object from result object
    3-Get the user info as String from user object .

    let response = responseObject as? [String: AnyObject]
    let result = response!["result"]  as? [String: AnyObject]
    
    if let user = result!["user"]  as? [String: AnyObject] {
            let displayName = user["display_name"] as? String
            let email = user["email"] as? String
    }
    
    0 讨论(0)
提交回复
热议问题