2017-09-22 34 views
0

的JSON看起來是這樣的:如何使用不同的鍵從JSON獲取數據?

{ 
"00AK": { 
    "icao": "00AK", 
    "iata": "", 
    "name": "Lowell Field", 
    "city": "Anchor Point", 
    "country": "US", 
    "elevation": 450, 
    "lat": 59.94919968, 
    "lon": -151.695999146, 
    "tz": "America\/Anchorage" 
}, 
"00AL": { 
    "icao": "00AL", 
    "iata": "", 
    "name": "Epps Airpark", 
    "city": "Harvest", 
    "country": "US", 
    "elevation": 820, 
    "lat": 34.8647994995, 
    "lon": -86.7703018188, 
    "tz": "America\/Chicago" 
}, 
"00AZ": { 
    "icao": "00AZ", 
    "iata": "", 
    "name": "Cordes Airport", 
    "city": "Cordes", 
    "country": "US", 
    "elevation": 3810, 
    "lat": 34.3055992126, 
    "lon": -112.1650009155, 
    "tz": "America\/Phoenix" 
} 
} 

正如你可以看到不同的鍵 「00AK」, 「00AL」, 「00AZ」,等等。我如何解析這種JSON格式?

+0

參考 - https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types – user1046037

+0

什麼鍵之至解析?只是像解析其他json一樣解析json,如何從解析的json中檢索數據,確定你的實際問題? – luk2302

+0

你可以谷歌它搜索關鍵字jsonObject。那裏有大量的樣品。祝你好運! –

回答

0
let jsonData = //JSON DATA HERE 
do { 
    let dict = try NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as! NSDictionary 
    for (key, value) in dict { 
     let subDict = value as! NSDictionary 
     //Then you can access the values from subDict 
    } catch { 
     //ERROR HANDLING  
    } 
+1

你不應該在Swift中使用NSDictionary,最好使用像'[String:Any]'這樣的Swift詞典。你也不應該強制施放,而是應對可能的施法失敗。 – Moritz

+0

同意。我只是給出了這個過程的基本要點。 – Malik

0

所以這裏我聲明一個結構如下

struct Model { 
var iaco: String? 
var iata: String? 
var name: String? 
var city: String? 
var country: String? 
var elevation: Int? 
var lat: Double? 
var lon: Double? 
var tz: String? } 

然後在陣列聲明以保持響應結果

var listOfModels = Array<Model>() 

然後取密鑰的列表從響應字典和迭代它以獲得結果並將其存儲在數組中

handleResponse { (response) in 
     for key in response.keys { 
      let dict = response[key] as? [String:Any] 
      var model = Model() 
      model.iaco = dict?["icao"] as? String 
      model.iata = dict?["iata"] as? String 
      model.name = dict?["name"] as? String 
      model.city = dict?["city"] as? String 
      model.country = dict?["country"] as? String 
      model.elevation = dict?["elevation"] as? Int 
      model.lat = dict?["lat"] as? Double 
      model.lon = dict?["lon"] as? Double 
      model.tz = dict?["tz"] as? String 
      listOfModels.append(model) 
     } 
    } 

response.keys用於從字典中獲取密鑰列表。

0

你可以試試下面的代碼片段:

func parseData() { 
    let jsonData = Data() /// your actual response data goes here... 
    do { 
     let dict = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) 
     guard let swiftDict = dict as? [String : Any] else { 
      print("Not a valid response") 
      return 
     } 

     for (key, value) in swiftDict { 
      guard let valueDict = value as? [String: Any] else { 
       /// handle improper response here 
       return 
      } 

      /// Got the actual dictionary in 'valueDict'... 
     } 
    } 
    catch { 
      /// handle parsing error here 
    } 

}