使用Swift 3解析另一个JSON数组中的一个对象的JSON数组

时间:2017-07-02 10:56:52

标签: json parsing swift3

我是Swift和iOS的新手,我正在学习使用REST API调用。虽然通话有效,但我能够获取数据。我无法正确解析整个数据。我想获取国家和他们的呼叫代码。我成功地解析了国家名称并将它们存储在一个数组中但由于某种原因我无法解析包含每个国家/地区的调用代码的数组对象。

这是我从API获得的JSON响应

[
{
name: "Afghanistan",
topLevelDomain: [
".af"
],
alpha2Code: "AF",
alpha3Code: "AFG",
callingCodes: [
"93"
],
capital: "Kabul",
altSpellings: [
"AF",
"Afġānistān"
],
region: "Asia",
subregion: "Southern Asia",
population: 27657145,
latlng: [
33,
65
],
demonym: "Afghan",
area: 652230,
gini: 27.8,
timezones: [
"UTC+04:30"
],
borders: [
"IRN",
"PAK",
"TKM",
"UZB",
"TJK",
"CHN"
],
nativeName: "افغانستان",
numericCode: "004",
currencies: [
{
code: "AFN",
name: "Afghan afghani",
symbol: "؋"
}
],
languages: [
{
iso639_1: "ps",
iso639_2: "pus",
name: "Pashto",
nativeName: "پښتو"
},
{
iso639_1: "uz",
iso639_2: "uzb",
name: "Uzbek",
nativeName: "Oʻzbek"
},
{
iso639_1: "tk",
iso639_2: "tuk",
name: "Turkmen",
nativeName: "Türkmen"
}
],
translations: {
de: "Afghanistan",
es: "Afganistán",
fr: "Afghanistan",
ja: "アフガニスタン",
it: "Afghanistan",
br: "Afeganistão",
pt: "Afeganistão"
},
flag: "https://restcountries.eu/data/afg.svg",
regionalBlocs: [
{
acronym: "SAARC",
name: "South Asian Association for Regional Cooperation",
otherAcronyms: [ ],
otherNames: [ ]
}
]
},
.....

]

这是我尝试过的事情

    func getData2() {
    let urlString = "https://restcountries.eu/rest/v2/all"

    let url = URL(string: urlString)
    URLSession.shared.dataTask(with:url!) { (data, response, error) in
        if error != nil {
            print(error!)
        } else {
            do {

                let parsedData = try JSONSerialization.jsonObject(with: data!) as! [[String:Any]]
               // let countryNames = parsedData.flatMap { $0["name"] as? String }
                //let countryCodes = parsedData.flatMap { $0["callingCodes"] as? String }

                for country in parsedData {

                    if let name = country["name"] as? String {
                        self.countryNamesArray.append(name)
                    }
                    if let capital = country["capital"] as? String {
                       print(capital)
                    }

                    if let code = country["callingCodes"] as? [[String:Any]] {

                        print("Got Here: ", code)
                        // why I get nothing here?

                        for tag in code {
                            print(tag)
                        // I get nothing here as well
                           }
                        }
                        //self.countryCodesArray.append(code)
                    }
                }

                //print(self.countryNamesArray)
               // print(self.countryCodesArray)
                self.searchTextField.filterStrings(self.countryNamesArray)

            } catch let error as NSError {
                print(error)
            }
        }

        }.resume()
}

来自android背景,我认为可以索引数组并获取该位置的项目,但是这也不起作用。请不要介意,因为我只是一个初学者,并尽力学习Swift。

任何帮助都将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

对您的代码进行此更改

 if let code = country["callingCodes"] as? [String] {

                      // if you want only first index use this
                      if code.count > 0{
                        let countryCode = code[0]
                        print("First index Country Code",countryCode)
                        self.countryCodesArray.append(countryCode)
                      }
}