ios - Parsing JSON with dynamic keys in Swift -
this question has answer here:
i trying parse json files url. json has key dynamic, in changes per file. right code looks this:
let json = try nsjsonserialization.jsonobjectwithdata(data!, options:.allowfragments) // parsing if let stations = json["observations"]!!["-10019482"] as? [[string: anyobject]] { observation in stations { if let name = observation["stationname"] as? string { if let temperatuuri = observation["temperature"] as? string { if let windspeed = observation["windspeedms"] as? string { print(name, temperatuuri, windspeed,"m/s") self.temperature.text = string(temperatuuri) } } } } }
"-10019482" part changed dynamically , key name cannot predicted. however, know first key inside "observations".
how same, without searching specific key name?
since json["observations"]
dictionary obtain it's first key
guard let dict = json["observations"] as? [string: anyobject], let firstkey = dict.keys.first else { return } print(firstkey) // should print first key found in observations dictionary
Comments
Post a Comment