スウィフト3、Xcode8.2.1、スウィフト3解析値
私はプロジェクト内のJSONファイルから特定の値を抽出しようとしています。ファイルの名前はcity.list.jsonで、次のようにJSONファイルの構文は次のとおりです。
{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}}
{"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}
私が持っている入力が国名であると私は返されたid値または関連する国コードを必要とします文字列として。私が書いた
"Type 'Any?' has no subscript members",
方法:
private func findCountryCodeBy(location: String)->String{
var result:String="";
let bundle = Bundle(for: type(of: self));
if let theURL = bundle.url(forResource: "city.list", withExtension: "json") {
do {
let data = try Data(contentsOf: theURL);
if let parsedData = try? JSONSerialization.jsonObject(with: data, options:[]) as! [String:Any] {
result = parsedData["_id"][location][0] as! String;
}
} catch {
print(error);
result = "error";
}
}
return result;
}