我不确定我在哪里做错了。
let recievedJson = json
let results = recievedJson["results"] as! NSArray
let resultsDic = results[0] as! NSDictionary
let address = resultsDic["address_components"] as? NSArray
let zipcodeDic = address?[0] as! NSDictionary
let cityNameDic = address?[1] as! NSDictionary
let countyDic = address?[2] as! NSDictionary
let stateDic = address?[3] as! NSDictionary
let countryDic = address?[4] as! NSDictionary
let zipcode = zipcodeDic["long_name"] as! String
let cityName = cityNameDic["long_name"] as! String
let countyName = countyDic["long_name"] as! String
let stateName = stateDic["long_name"] as! String
let stateShortName = stateDic["short_name"] as! String
let countryName = countryDic["long_name"] as! String
let countryShortName = countryDic["short_name"] as! String
尝试从此json响应中检索数据
http://maps.googleapis.com/maps/api/geocode/json?address=23508&sensor=true
这对我有用,对@Danh答案的小改动。
let recievedJson: [String: Any] = json as! [String : Any]
if let results = recievedJson["results"] as? [[String: Any]] {
if results.count > 0 {
let resultsDic = results[0]
if let address = resultsDic["address_components"] as? [[String: Any]] {
for dict in address {
if let longName = dict["long_name"] as? String,
let shortName = dict["short_name"] as? String,
let types = dict["types"] as? [String] {
if types.contains("postal_code") {
print("postal_code: \(longName)")
} else if types.contains("locality") {
print("city: \(longName)")
} else if types.contains("administrative_area_level_2") {
print("county: \(longName)")
} else if types.contains("administrative_area_level_1") {
print("state: \(longName)")
} else if types.contains("country") {
print("country: \(longName)")
}
}
}
}
}
}
答案 0 :(得分:0)
address
数组有4个项目(索引从0到3)但是你访问的项目的索引是4,所以你得到了这个错误。您应该使用Optional Binding - if let
来解包值并在下标之前检查数组的count
。并且让我们使用Swift类型。代码必须是:
let recievedJson: [String: Any] = json
if let results = recievedJson["results"] as? [[String: Any]] {
if results.count > 0 {
let resultsDic = results[0]
if let address = resultsDic["address_components"] as? [[String: Any]] {
// zipcode
if address.count > 0 {
let zipcodeDic = address[0]
if let zipcode = zipcodeDic["long_name"] as? String {
print("zipcode: \(zipcode)")
}
}
// cityName
if address.count > 1 {
let cityNameDic = address[1]
if let cityName = cityNameDic["long_name"] as? String {
print("cityName: \(cityName)")
}
}
// stateName
if address.count > 2 {
let stateDic = address[2]
if let stateName = stateDic["long_name"] as? String {
print("stateName: \(stateName)")
}
if let stateShortName = stateDic["short_name"] as? String {
print("stateShortName: \(stateShortName)")
}
}
// countryDic
if address.count > 3 {
let countryDic = address[3]
if let countryName = countryDic["long_name"] as? String {
print("countryName: \(countryName)")
}
if let countryShortName = countryDic["short_name"] as? String {
print("countryShortName: \(countryShortName)")
}
}
}
}
}
更新:从Google Maps API提取地址组件
let recievedJson: [String: Any] = json
if let results = recievedJson["results"] as? [[String: Any]] {
if results.count > 0 {
let resultsDic = results[0]
if let address = resultsDic["address_components"] as? [[String: Any]] {
for dict in address {
if let longName = dict["long_name"] as? String,
let shortName = dict["short_name"] as? String,
let types = dict["types"] as? [String] {
if types.contains("postal_code") {
print("postal_code: \(longName)")
} else if types.contains("locality") {
print("city: \(longName)")
} else if types.contains("administrative_area_level_2") {
print("county: \(longName)")
} else if types.contains("administrative_area_level_1") {
print("state: \(longName)")
} else if types.contains("country") {
print("country: \(longName)")
}
}
}
}
}
}