How to get value of json data using SwiftyJSON

2k views Asked by At

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!

1

There are 1 answers

0
Eric Aya On BEST ANSWER

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 {.