如何使用swift解析字典中的Any [英] how do I parse Any in dictionary using swift

查看:104
本文介绍了如何使用swift解析字典中的Any的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是如何解析类型为的字典值. 我能够读取为字符串的键,其值是Any类型,并且在给定键的示例values以下

I'm not how do it parse a dictionary value which is of type . I'm able to read the key which is string and value is type of Any and has below sample values for given key

▿ 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

从上面,我只需要提取名称":爱沙尼亚语" 疲倦的循环使用swift无效.

From above, I only need to extract "name":"Estonian" Tired looping it did not work using 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"}]

推荐答案

请勿使用Any.不要使用NSArray.不要使用NSDictionary.这是斯威夫特!使用Swift类型和JSON的Swift解码.

Do not use Any. Do not use NSArray. Do not use NSDictionary. This is Swift! Use Swift types and Swift decoding of the JSON.

以下是您作为数据对象的JSON:

Here is your JSON as a Data object:

[
    {
     "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)!

以下是从中提取语言名称的方法:

Here is how to extract the language name from it:

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

这篇关于如何使用swift解析字典中的Any的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆