jsonswift2alamofireswifty-jsonvk-sdk

How to get value of json data using SwiftyJSON


This is JSON data

{"response":[{"uid":1,"first_name":"Павел","last_name":"Дуров","hidden":1}]}

How to get "first_name" value using SwiftyJSON

i tried so

Alamofire.request(.GET, "https://api.vk.com/method/users.get?", parameters: ["user_id": ID])
            .responseJSON { response in


                if let jsonData = response.result.value {
                    let first_name = JSON(jsonData)["first_name"].string
                    print("First name = \(first_name)")

                }
        }

but in output i have this: First name = nil

please help!


Solution

  • The value for your "response" key is an array.

    let result = JSON(jsonData)["response"].arrayValue
    let first_name = result[0]["first_name"].string
    

    Remember, JSON arrays begin with [ and JSON dictionaries begin with {.