HTTPTask response into Swifty for JSON serializati

2019-09-05 22:00发布

问题:

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 has a JSON Serializer, which I got working, but I rather use Swifty, it's seems easier to work with.

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)")
})

回答1:

SwiftyJSON is quite happy to take a variety of objects, including Dictionary?, so just go for it!

let dict = JSON(response.responseObject)


标签: json xcode swift