我正在尝试从openweathermap.org访问json数据。当我尝试从中访问任何变量或方法时,我总是收到错误消息。
我已经尝试过此方法Type "Any" has no subscript members in Swift 3 during pulling array of data from server,但是它不能解决我的问题。
这是我的代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=d6394afc8ed67fba1aa3bd98ea0d7230")!
let task = URLSession.shared.dataTask(with: url){(data, response, error) in
if error != nil{
print(error!)
}
else {
if let urlContent = data{
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)
print(jsonResult)
if let description = jsonResult["weather"]??[0]["description"]{
print(description)
}
}
catch {
print("json processing failed")
}
}
}
}
task.resume()
}
}
我在
处出错if let description = jsonResult["weather"]??[0]["description"]{
print(description)
}
这是数据:
{“ coord”:{“ lon”:-0.13,“ lat”:51.51},“ weather”:[{“ id”:300,“ main”:“毛毛雨”,“ description”:“光强度下毛毛雨“,” icon“:” 09d“}],” base“:” stations“,” main“:{” temp“:280.32,” pressure“:1012,”湿度“:81,” temp_min“:279.15, “ temp_max”:281.15},“可见性”:10000,“风”:{“速度”:4.1,“度”:80},“云”:{“全部”:90},“ dt”:1485789600,“ sys“:{” type“:1,” id“:5091,” message“:0.0103,” country“:” GB“,” sunrise“:1485762037,” sunset“:1485794875},” id“:2643743,” name“:” London“,” cod“:200}
我想在“天气”下打印“说明”。