JSON对象数组内无法访问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? – dirtydanee
让我们一块一块地做。 'let weatherValue = newValue [“weather”]'returns?如果它返回正确,'let firstWeather = weatherValue [0]'返回?如果返回正确,'let weatherDescription = firstWeather [“description”]'返回? – Larme
其不起作用 其给出错误 “类型”任何?有没有下成员” 是可能的,因为我正在转换结果为NSDictionary的 因为我想大多数的选项和它不工作,并给这个错误 –