The JSON looks like this:
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"
}
}
As you can see the keys varies "00AK", "00AL", "00AZ", and so on. How do I parse this format of JSON?
如您所见,按键变化为“00AK”,“00AL”,“00AZ”等。如何解析这种JSON格式?
3 个解决方案
#1
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
}
#2
0
So here it is I declare one structure as following
所以我在这里声明一个结构如下
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? }
Then declare on array to hold the response result
然后在数组上声明以保存响应结果
var listOfModels = Array<Model>()
Then take a list of keys from response Dictionary and iterate over it to get result and store it in array
然后从响应字典中获取一个键列表并迭代它以获得结果并将其存储在数组中
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 is used to get list of keys from dictionary.
response.keys用于从字典中获取键列表。
#3
0
You could try the below snippet:
您可以尝试下面的代码段:
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
}
}
#1
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
}
#2
0
So here it is I declare one structure as following
所以我在这里声明一个结构如下
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? }
Then declare on array to hold the response result
然后在数组上声明以保存响应结果
var listOfModels = Array<Model>()
Then take a list of keys from response Dictionary and iterate over it to get result and store it in array
然后从响应字典中获取一个键列表并迭代它以获得结果并将其存储在数组中
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 is used to get list of keys from dictionary.
response.keys用于从字典中获取键列表。
#3
0
You could try the below snippet:
您可以尝试下面的代码段:
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
}
}