访问 Wunderground API 的 "dailysummary" 部分时出现问题
Issues Accessing "dailysummary" section of Wunderground API
我创建了一组函数来从 Wunderground API 检索数据。但是,由于 "dailysummary" 部分以某种方式包含在数组中,我不知道如何访问它。这是我无法访问的部分:
"history": {
"dailysummary": [
{ "date": {
"pretty": "12:00 PM PDT on August 12, 2015",
"year": "2015",
"mon": "08",
"mday": "12",
"hour": "12",
"min": "00",
"tzname": "America/Los_Angeles"
},
"fog":"0","rain":"0","snow":"0","snowfallm":"0.00","snowfalli":"0.00","monthtodatesnowfallm":"", "monthtodatesnowfalli":"","since1julsnowfallm":"", "since1julsnowfalli":"","snowdepthm":"", "snowdepthi":"","hail":"0","thunder":"0","tornado":"0","meantempm":"26", "meantempi":"79","meandewptm":"16", "meandewpti":"60","meanpressurem":"1014", "meanpressurei":"29.94","meanwindspdm":"9", "meanwindspdi":"5","meanwdire":"","meanwdird":"331","meanvism":"16", "meanvisi":"10","humidity":"","maxtempm":"33", "maxtempi":"91","mintempm":"19", "mintempi":"66","maxhumidity":"78","minhumidity":"34","maxdewptm":"17", "maxdewpti":"62","mindewptm":"15", "mindewpti":"59","maxpressurem":"1016", "maxpressurei":"30.01","minpressurem":"1012", "minpressurei":"29.88","maxwspdm":"24", "maxwspdi":"15","minwspdm":"0", "minwspdi":"0","maxvism":"16", "maxvisi":"10","minvism":"16", "minvisi":"10","gdegreedays":"28","heatingdegreedays":"0","coolingdegreedays":"14","precipm":"0.00", "precipi":"0.00","precipsource":"","heatingdegreedaysnormal":"0","monthtodateheatingdegreedays":"0","monthtodateheatingdegreedaysnormal":"0","since1sepheatingdegreedays":"","since1sepheatingdegreedaysnormal":"","since1julheatingdegreedays":"0","since1julheatingdegreedaysnormal":"17","coolingdegreedaysnormal":"5","monthtodatecoolingdegreedays":"106","monthtodatecoolingdegreedaysnormal":"69","since1sepcoolingdegreedays":"","since1sepcoolingdegreedaysnormal":"","since1jancoolingdegreedays":"600","since1jancoolingdegreedaysnormal":"280" }
]
}
有关我如何调用 API 的参考,请参阅此站点:http://devdactic.com/rest-api-parse-json-swift/
您可能正在使用 SwiftyJSON 或其他库,但对于我的示例,我只是使用 NSJSONSerialization.
在这里,我进入 "history" 字典,然后进入 "dailysummary" 字典,并将结果转换为字典数组(假设您的 JSON 字典名为 "json" 也是):
if let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &err) as? [String:AnyObject] {
if let history = json["history"] as? [String:AnyObject],
let daily = history["dailysummary"] as? [[NSObject:AnyObject]] {
// "daily" is our array
}
}
然后我在数组中循环(在你的例子中,里面只有一个对象,但可能有很多):一些值是字符串,其他是字典,比如 "date".
我正在使用 "if let" 来安全地展开和投射内容:
if let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &err) as? [String:AnyObject] {
if let history = json["history"] as? [String:AnyObject],
let daily = history["dailysummary"] as? [[NSObject:AnyObject]] {
for item in daily {
for (key, _) in item {
println(key) // "mintempm", "mindewpti", "since1sepheatingdegreedaysnormal", "meantempm", etc
}
if let coolingdegreedaysnormal = item["coolingdegreedaysnormal"] as? String {
println(coolingdegreedaysnormal) // "5"
}
if let date = item["date"] as? [String:AnyObject], let pretty = date["pretty"] as? String {
println(pretty) // "12:00 PM PDT on August 12, 2015"
}
}
}
}
如果您使用的是 SwiftyJSON 或等效工具,则不必手动进行强制转换,但您必须使用特定于库的语法,也许是这种风格的语法:
for item in json["history"]["dailysummary"].array {
// ...
}
我创建了一组函数来从 Wunderground API 检索数据。但是,由于 "dailysummary" 部分以某种方式包含在数组中,我不知道如何访问它。这是我无法访问的部分:
"history": {
"dailysummary": [
{ "date": {
"pretty": "12:00 PM PDT on August 12, 2015",
"year": "2015",
"mon": "08",
"mday": "12",
"hour": "12",
"min": "00",
"tzname": "America/Los_Angeles"
},
"fog":"0","rain":"0","snow":"0","snowfallm":"0.00","snowfalli":"0.00","monthtodatesnowfallm":"", "monthtodatesnowfalli":"","since1julsnowfallm":"", "since1julsnowfalli":"","snowdepthm":"", "snowdepthi":"","hail":"0","thunder":"0","tornado":"0","meantempm":"26", "meantempi":"79","meandewptm":"16", "meandewpti":"60","meanpressurem":"1014", "meanpressurei":"29.94","meanwindspdm":"9", "meanwindspdi":"5","meanwdire":"","meanwdird":"331","meanvism":"16", "meanvisi":"10","humidity":"","maxtempm":"33", "maxtempi":"91","mintempm":"19", "mintempi":"66","maxhumidity":"78","minhumidity":"34","maxdewptm":"17", "maxdewpti":"62","mindewptm":"15", "mindewpti":"59","maxpressurem":"1016", "maxpressurei":"30.01","minpressurem":"1012", "minpressurei":"29.88","maxwspdm":"24", "maxwspdi":"15","minwspdm":"0", "minwspdi":"0","maxvism":"16", "maxvisi":"10","minvism":"16", "minvisi":"10","gdegreedays":"28","heatingdegreedays":"0","coolingdegreedays":"14","precipm":"0.00", "precipi":"0.00","precipsource":"","heatingdegreedaysnormal":"0","monthtodateheatingdegreedays":"0","monthtodateheatingdegreedaysnormal":"0","since1sepheatingdegreedays":"","since1sepheatingdegreedaysnormal":"","since1julheatingdegreedays":"0","since1julheatingdegreedaysnormal":"17","coolingdegreedaysnormal":"5","monthtodatecoolingdegreedays":"106","monthtodatecoolingdegreedaysnormal":"69","since1sepcoolingdegreedays":"","since1sepcoolingdegreedaysnormal":"","since1jancoolingdegreedays":"600","since1jancoolingdegreedaysnormal":"280" }
]
}
有关我如何调用 API 的参考,请参阅此站点:http://devdactic.com/rest-api-parse-json-swift/
您可能正在使用 SwiftyJSON 或其他库,但对于我的示例,我只是使用 NSJSONSerialization.
在这里,我进入 "history" 字典,然后进入 "dailysummary" 字典,并将结果转换为字典数组(假设您的 JSON 字典名为 "json" 也是):
if let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &err) as? [String:AnyObject] {
if let history = json["history"] as? [String:AnyObject],
let daily = history["dailysummary"] as? [[NSObject:AnyObject]] {
// "daily" is our array
}
}
然后我在数组中循环(在你的例子中,里面只有一个对象,但可能有很多):一些值是字符串,其他是字典,比如 "date".
我正在使用 "if let" 来安全地展开和投射内容:
if let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &err) as? [String:AnyObject] {
if let history = json["history"] as? [String:AnyObject],
let daily = history["dailysummary"] as? [[NSObject:AnyObject]] {
for item in daily {
for (key, _) in item {
println(key) // "mintempm", "mindewpti", "since1sepheatingdegreedaysnormal", "meantempm", etc
}
if let coolingdegreedaysnormal = item["coolingdegreedaysnormal"] as? String {
println(coolingdegreedaysnormal) // "5"
}
if let date = item["date"] as? [String:AnyObject], let pretty = date["pretty"] as? String {
println(pretty) // "12:00 PM PDT on August 12, 2015"
}
}
}
}
如果您使用的是 SwiftyJSON 或等效工具,则不必手动进行强制转换,但您必须使用特定于库的语法,也许是这种风格的语法:
for item in json["history"]["dailysummary"].array {
// ...
}