【发布时间】:2017-03-20 15:28:29
【问题描述】:
无法访问 json 对象,它是 json 对象中的数组 我想从数组内有数组的json对象访问数据 并且那个json文件也被上传了 所以请任何人都可以检查并帮助我如何获得“weather.description” 数据
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=13ae70c6aefa867c44962edc13f94404")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print("some error occured")
} else {
if let urlContent = data {
do{
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)
let newValue = jsonResult as! NSDictionary
print(jsonResult)
let name = newValue["name"]
//Here i am getting name as variable value
//this is not working
let description = newValue["weather"]??[0]["description"]
//this is not working
let description = newValue["weather"]!![0]["description"]
print()
}catch {
print("JSON Preocessing failed")
}
}
}
}
task.resume()
}
【问题讨论】:
-
您使用的是哪个 Swift 版本? 3.0?
-
让我们一块一块地做。
let weatherValue = newValue["weather"]退货?如果返回正确,let firstWeather = weatherValue[0]返回?如果返回正确,let weatherDescription = firstWeather["description"]是否返回? -
它不工作它给出错误“类型'Any?'没有下标成员”是可能的,因为我正在将结果转换为 NSDictionary 因为我尝试了大多数选项并且它不起作用并给出了这个错误
标签: ios arrays swift dictionary swift3