HTTPTask 响应到 Swifty 以进行 JSON 序列化 [英] HTTPTask response into Swifty for JSON serialization
问题描述
我正在使用 HTTPTask 从 openweathermap.org 加载数据.哪个工作正常.我在将数据转换为 JSON 时遇到问题.我想使用 SwiftyJSON 但是,我不太清楚如何将两者连接起来.
I'm using HTTPTask to load data from openweathermap.org. Which is working fine. I'm having trouble converting the data to JSON. I'd like to use SwiftyJSON but, I can't quite figure out how to bridge the two.
HTTPTask 有一个 JSON 序列化器,我开始使用它,但我更喜欢使用 Swifty,它似乎更容易使用.
HTTPTask has a JSON Serializer, which I got working, but I rather use Swifty, it's seems easier to work with.
这是我目前所拥有的.这会从 openweathermap.org 加载天气.我不确定如何将响应传递给 Swifty.
Here's what I have so far. This loads the weather from openweathermap.org. I'm not sure how to pass the response into Swifty.
var request = HTTPTask()
request.requestSerializer = JSONRequestSerializer()
request.responseSerializer = JSONResponseSerializer()
request.GET(openWeatherURL, parameters: ["q":"San Francisco", "APPID":openWeatherAPIKey], success: {(response: HTTPResponse) in
if let dict = response.responseObject as? Dictionary<String, AnyObject> {
println("Response: \(response)")
println("Dictionary: \(dict)")
let description = dict["weather"]["description"]
println(description)
}
}, failure: {(error: NSError, repsonse: HTTPResponse?) in
println("error \(error)")
})
推荐答案
SwiftyJSON 很乐意接受各种对象,包括 Dictionary?,所以就去做吧!
SwiftyJSON is quite happy to take a variety of objects, including Dictionary?, so just go for it!
let dict = JSON(response.responseObject)
这篇关于HTTPTask 响应到 Swifty 以进行 JSON 序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!