访问Wunderground API中的值

时间:2015-08-11 18:59:00

标签: swift wunderground

我正在尝试将Wunderground纳入我当前的项目中。我已经查看了几个api教程,但我似乎无法弄清楚如何访问API的某个部分。例如,这就是API的样子:

{
   "response": {
   "version":"0.1",
   "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
   "features": {
      "history": 1
   }
   }
    ,
"history": {
    "date": {
    "pretty": "August 9, 2015",
    "year": "2015",
    "mon": "08",
    "mday": "09",
    "hour": "12",
    "min": "00",
    "tzname": "America/Los_Angeles"
    },

我们想说我只想从API返回一小时。我该怎么做?

1 个答案:

答案 0 :(得分:1)

一种在没有框架的情况下解析JSON的方法:

typealias JSONdic = [String: AnyObject]

NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: nsUrl), queue: NSOperationQueue.mainQueue(), completionHandler: { (_, data, _) -> Void in
    if let data = data, json = data as? JSONdic, history = json["history"] as? JSONdic, hour = history["hour"] as? String {
    println(hour)
}