ctabuyo ctabuyo - 11 months ago 58
iOS Question

Can´t parse JSON with AlamoFire and SwiftyJSON

I am stuck with parsing JSON with AlamoFire and SwiftyJSON for iOS. I have a JSON such as this one:

[{"id":23561,"name":"RFI - Persan رادیو صدای Ùرانسه Ùارسی","country":"FR","image":{"url":null,"thumb":{"url":null}},"slug":"rfi-persan-رادیو-صدای-Ùرانسه-Ùارسی","website":"rfi","twitter":"","facebook":"","categories":[{"id":21,"title":"News","description":"","slug":"news","ancestry":"4"}],"streams":[{"stream":"http://rfi-persan.scdn.arkena.com/rfienpersan.mp3","bitrate":0,"content_type":"audio/mpeg","status":1}],"created_at":"2016-01-12T07:52:08+01:00","updated_at":"2016-08-02T01:52:50+02:00"}]

This is what I´ve tried so far, but doesn't work:

func loadSomeJSONData() {
Alamofire.request(.GET, "http://example.com/json/")
.responseJSON { (_, _, data, _) in
let json = JSON(data!)
if let Name = json["name"].string {
println("name: \(firstName)") // Name should equal "RFI"

But for some reason it doesn't work.

Thank you very much!


Your json is Array not Dictionary, so access the json this way

if let arr = json.arrayObject as? [[String:AnyObject]] { 
    if let name = arr[0]["name"] as? String {
        println("name: \(name)") // Name should equal "RFI"