Skip to content

Instantly share code, notes, and snippets.

@satomixx
Last active August 29, 2015 14:24
Show Gist options
  • Save satomixx/594acd6aa643fd0ec98d to your computer and use it in GitHub Desktop.
Save satomixx/594acd6aa643fd0ec98d to your computer and use it in GitHub Desktop.
[Swift] AlamofireでApiを叩いて、SwiftyJSONでレスポンスデータを展開する ref: http://qiita.com/tsumekoara/items/9bbb429f41e40c76882c
import UIKit
import Alamofire
import SwiftyJSON
    ・
    ・
    ・
Alamofire.request(.GET, "http://foo.com/api/v1/bar/1", parameters: ["foo": "bar"])
.response { (request, response, data, error) in
println(request)
println(response)
}
.responseJSON { (_, _, json_data, _) in
println(json_data)
let json = JSON(json_data!)
println(json)
println(json["bar"])
println(json["bar"]["hoge_id"])
}
}
}
{
"bar" : {
"id" : 1,
"created_at" : "2015-07-01T12:25:41.438+09:00",
"updated_at" : "2015-07-01T12:25:41.438+09:00",
"hoge_id" : 1
}
}
"id" : 1,
"created_at" : "2015-07-01T12:25:41.438+09:00",
"updated_at" : "2015-07-01T12:25:41.438+09:00",
"hoge_id" : 1
Optional({
bar = {
"created_at" = "2015-07-01T12:25:41.438+09:00";
id = 1;
"updated_at" = "2015-07-01T12:25:41.438+09:00";
"hoge_id = 1"
};
})
<NSMutableURLRequest: 0x7fab3b28b720> { URL: http://foo.com/api/v1/bar/1?foo=bar }
Optional(<NSHTTPURLResponse: 0x7fab38d60bc0> { URL: http://foo.com/api/v1/bar/1?foo=bar } { status code: 200, headers {
   ・
   ・
"Cache-Control" = "max-age=0, private, must-revalidate";
Connection = "keep-alive";
"Content-Type" = "application/json; charset=utf-8";
   ・
   ・
   ・
} })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment