如何使用 swift 解析字典中的 Any
how do I parse Any in dictionary using swift
我不知道它如何解析类型为 的字典值。
我能够读取是字符串的键,值是 Any 类型,并且对于给定的键
有以下示例 values
▿ 1 element
▿ 0 : 4 elements
▿ 0 : 2 elements
- key : nativeName
- value : Shqip
▿ 1 : 2 elements
- key : iso639_2
- value : sqi
▿ 2 : 2 elements
- key : name
- value : Albanian
▿ 3 : 2 elements
- key : iso639_1
- value : sq
从上面,我只需要提取"name":"Estonian"
使用 swift.
累了循环它不起作用
代码:
f(key == “languages”){
var nameArray = value as! NSArray
for str in nameArray{
print(str)
}
}
完成 JSON 回复
[{"name":"Estonia","topLevelDomain":[".ee"],"alpha2Code":"EE","alpha3Code":"EST","callingCodes":["372"],"capital":"Tallinn","altSpellings":["EE","Eesti","Republic of Estonia","Eesti Vabariik"],"region":"Europe","subregion":"Northern Europe","population":1315944,"latlng":[59.0,26.0],"demonym":"Estonian","area":45227.0,"gini":36.0,"timezones":["UTC+02:00"],"borders":["LVA","RUS"],"nativeName":"Eesti","numericCode":"233","currencies":[{"code":"EUR","name":"Euro","symbol":"€"}],"languages":[{"iso639_1":"et","iso639_2":"est","name":"Estonian","nativeName":"eesti"}],"translations":{"de":"Estland","es":"Estonia","fr":"Estonie","ja":"エストニア","it":"Estonia","br":"Estônia","pt":"Estónia","nl":"Estland","hr":"Estonija","fa":"استونی"},"flag":"https://restcountries.eu/data/est.svg","regionalBlocs":[{"acronym":"EU","name":"European Union","otherAcronyms":[],"otherNames":[]}],"cioc":"EST"}]
不要使用任何。不要使用 NSArray。不要使用 NSDictionary。这是Swift!使用 Swift 类型和 Swift 解码 JSON。
这是您的 JSON 作为数据对象:
[
{
"name":"Estonia",
"topLevelDomain":[".ee"],
"alpha2Code":"EE",
"alpha3Code":"EST",
"callingCodes":["372"],
"capital":"Tallinn",
"altSpellings":["EE","Eesti","Republic of Estonia","Eesti Vabariik"],
"region":"Europe",
"subregion":"Northern Europe",
"population":1315944,
"latlng":[59.0,26.0],
"demonym":"Estonian",
"area":45227.0,
"gini":36.0,
"timezones":["UTC+02:00"],
"borders":["LVA","RUS"],
"nativeName":"Eesti",
"numericCode":"233",
"currencies":[{"code":"EUR","name":"Euro","symbol":"€"}],
"languages":[
{
"iso639_1":"et",
"iso639_2":"est",
"name":"Estonian",
"nativeName":"eesti"
}
],
"translations":
{
"de":"Estland",
"es":"Estonia",
"fr":"Estonie",
"ja":"エストニア",
"it":"Estonia",
"br":"Estônia",
"pt":"Estónia",
"nl":"Estland",
"hr":"Estonija",
"fa":"استونی"
},
"flag":"https://restcountries.eu/data/est.svg",
"regionalBlocs":[
{
"acronym":"EU",
"name":"European Union",
"otherAcronyms":[],
"otherNames":[]
}
],
"cioc":"EST"
}
]
"""
let data = json.data(using: .utf8)!
以下是从中提取语言名称的方法:
struct Language : Decodable {
let name : String
}
struct Entry : Decodable {
let languages : [Language]
}
let entries = try! JSONDecoder().decode([Entry].self, from: data)
let lang = entries[0].languages[0].name // Estonian
我不知道它如何解析类型为 的字典值。 我能够读取是字符串的键,值是 Any 类型,并且对于给定的键
有以下示例values
▿ 1 element
▿ 0 : 4 elements
▿ 0 : 2 elements
- key : nativeName
- value : Shqip
▿ 1 : 2 elements
- key : iso639_2
- value : sqi
▿ 2 : 2 elements
- key : name
- value : Albanian
▿ 3 : 2 elements
- key : iso639_1
- value : sq
从上面,我只需要提取"name":"Estonian" 使用 swift.
累了循环它不起作用代码:
f(key == “languages”){
var nameArray = value as! NSArray
for str in nameArray{
print(str)
}
}
完成 JSON 回复
[{"name":"Estonia","topLevelDomain":[".ee"],"alpha2Code":"EE","alpha3Code":"EST","callingCodes":["372"],"capital":"Tallinn","altSpellings":["EE","Eesti","Republic of Estonia","Eesti Vabariik"],"region":"Europe","subregion":"Northern Europe","population":1315944,"latlng":[59.0,26.0],"demonym":"Estonian","area":45227.0,"gini":36.0,"timezones":["UTC+02:00"],"borders":["LVA","RUS"],"nativeName":"Eesti","numericCode":"233","currencies":[{"code":"EUR","name":"Euro","symbol":"€"}],"languages":[{"iso639_1":"et","iso639_2":"est","name":"Estonian","nativeName":"eesti"}],"translations":{"de":"Estland","es":"Estonia","fr":"Estonie","ja":"エストニア","it":"Estonia","br":"Estônia","pt":"Estónia","nl":"Estland","hr":"Estonija","fa":"استونی"},"flag":"https://restcountries.eu/data/est.svg","regionalBlocs":[{"acronym":"EU","name":"European Union","otherAcronyms":[],"otherNames":[]}],"cioc":"EST"}]
不要使用任何。不要使用 NSArray。不要使用 NSDictionary。这是Swift!使用 Swift 类型和 Swift 解码 JSON。
这是您的 JSON 作为数据对象:
[
{
"name":"Estonia",
"topLevelDomain":[".ee"],
"alpha2Code":"EE",
"alpha3Code":"EST",
"callingCodes":["372"],
"capital":"Tallinn",
"altSpellings":["EE","Eesti","Republic of Estonia","Eesti Vabariik"],
"region":"Europe",
"subregion":"Northern Europe",
"population":1315944,
"latlng":[59.0,26.0],
"demonym":"Estonian",
"area":45227.0,
"gini":36.0,
"timezones":["UTC+02:00"],
"borders":["LVA","RUS"],
"nativeName":"Eesti",
"numericCode":"233",
"currencies":[{"code":"EUR","name":"Euro","symbol":"€"}],
"languages":[
{
"iso639_1":"et",
"iso639_2":"est",
"name":"Estonian",
"nativeName":"eesti"
}
],
"translations":
{
"de":"Estland",
"es":"Estonia",
"fr":"Estonie",
"ja":"エストニア",
"it":"Estonia",
"br":"Estônia",
"pt":"Estónia",
"nl":"Estland",
"hr":"Estonija",
"fa":"استونی"
},
"flag":"https://restcountries.eu/data/est.svg",
"regionalBlocs":[
{
"acronym":"EU",
"name":"European Union",
"otherAcronyms":[],
"otherNames":[]
}
],
"cioc":"EST"
}
]
"""
let data = json.data(using: .utf8)!
以下是从中提取语言名称的方法:
struct Language : Decodable {
let name : String
}
struct Entry : Decodable {
let languages : [Language]
}
let entries = try! JSONDecoder().decode([Entry].self, from: data)
let lang = entries[0].languages[0].name // Estonian