2017-07-02 93 views
0

我對Swift和iOS非常陌生,並且正在學習如何使用REST API調用。電話正常工作,我可以獲取數據。我看不到正確解析整個數據。我正試圖獲得這些國家和他們的電話代碼。我成功解析了國名並將它們存儲在一個數組中,但出於某種原因,我無法解析包含每個國家/地區的調用代碼的數組對象。解析JSON使用Swift 3解析另一個JSON數組中的一個對象的數組3

這裏的JSON響應,我從API

[ 
{ 
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。

任何幫助,將不勝感激。

感謝

回答

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) 
         } 
} 
+0

真棒感謝,它的工作原理。我還有一個問題。一些呼叫代碼多於一個,例如[「1809」,「1829」,「1849」],但我只想使用第一個。我如何索引第一個值? –

+0

我會編輯我的答案,請看看 –

+0

謝謝。一些國家代碼是空的,但是即使在檢查字符串是否爲空之後,它也會發出索引錯誤。我如何檢查調用代碼是否存在以避免索引問題? –