Swift - Decoding a Deeply Nested Dictionary - swift

I am so close - but I am struggling with a very simple function to allow me to access a data point deeply nested in my JSON. The example I am using is on the Google directions API.
Sample JSON (from GMapsAPI):
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJ960bMolw44kRQcGOlOZQ-r8",
"types" : [ "premise" ]
},
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "EiMxMTggU2FsZW0gU3QsIEJvc3RvbiwgTUEgMDIxMTMsIFVTQSIaEhgKFAoSCSvDfDSJcOOJEbQanF0WxROfEHY",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 42.3647252,
"lng" : -71.0555085
},
"southwest" : {
"lat" : 42.3644965,
"lng" : -71.05552419999999
}
},
"copyrights" : "Map data ©2018 Google",
"legs" : [
{
"distance" : {
"text" : "82 ft",
"value" : 25
},
"duration" : {
"text" : "1 min",
"value" : 11
},
"end_address" : "118 Salem St, Boston, MA 02113, USA",
"end_location" : {
"lat" : 42.3647252,
"lng" : -71.0555085
},
"start_address" : "115 Salem St, Boston, MA 02113, USA",
"start_location" : {
"lat" : 42.3644965,
"lng" : -71.05552419999999
},
"steps" : [
{
"distance" : {
"text" : "82 ft",
"value" : 25
},
"duration" : {
"text" : "1 min",
"value" : 11
},
"end_location" : {
"lat" : 42.3647252,
"lng" : -71.0555085
},
"html_instructions" : "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eSalem St\u003c/b\u003e toward \u003cb\u003eJerusalem Pl\u003c/b\u003e",
"polyline" : {
"points" : "ciqaG~_upLO?]A"
},
"start_location" : {
"lat" : 42.3644965,
"lng" : -71.05552419999999
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "ciqaG~_upLm#A"
},
"summary" : "Salem St",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}
Decodable Structure: To work with this, I am using Decodable. I have been able to access first level nested data (routes.summary), but I am struggling to get further down (for example: routes.legs.duration). My code structure is as follows:
struct Directions: Decodable {
let status: String
let routes: [Routes]
enum CodingKeys :String, CodingKey {
case status, routes
}
struct Routes: Decodable {
let summary: String
let legs: [Legs]
enum CodingKeys : String, CodingKey {
case summary, legs
}
}
struct Legs: Decodable {
let duration: Duration
enum CodingKeys : String, CodingKey {
case duration
}
}
struct Duration: Decodable {
let text: String
enum CodingKeys : String, CodingKey {
case text
}
}
Implementation after URL set-up:
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do {
let directions = try
JSONDecoder().decode(Directions.self, from: data)
for item in directions.routes {
self.stringoutput = item.summary
}
After all this, all I want to do is be able to access "text" in the JSON and return that value. The last line in the code is able to successfully return "summary" in the JSON; and I can print(directions) and the whole array/dictionary will return in the debug area, including "text". But I still can't figure out how to do:
x = directions.routes.legs.duration.text
to make x equal to "1 min"
Would be appreciative of anyone's help.
Edit: What ended up working is Vadian's struct keys below and the following for in loop:
for item in directions.routes {
print(item.summary)
self.direct = item.summary
for items in item.legs {
self.stringoutput = items.duration.text
print(items.duration.text)
}
Cheers!

These structs don't decode all keys, but it's a starting point.
If keys and struct members have the same name you don't need to specify CodingKeys
struct Directions: Decodable {
let status: String
let routes: [Route]
}
struct Route: Decodable {
let summary: String
let legs: [Leg]
}
struct Leg: Decodable {
let duration : TextValue
let distance : TextValue
let endAddress : String
let endLocation : Location
let startAddress : String
let startLocation : Location
let steps : [Step]
}
struct TextValue: Decodable {
let text: String
let value : Int
}
struct Location: Decodable {
let lat, lng : Double
}
struct Step: Decodable {
let duration : TextValue
let distance : TextValue
let endLocation : Location
let startLocation : Location
let htmlInstructions : String
let travelMode : String
}
To decode the snake_cased keys properly you have to add the appropriate key decoding strategy
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
To access the arrays ([]) you have to get an item by index
step[0]
or iterate the array with a loop
for step in steps {}

Related

How parse json recursively using codable with swift

I am trying to defining a decoding class model to decode this kind of json file:
Here a short extraction to understand the problem, in reality it is more nested.
{
"Title" : "Root",
"Subtitle" : "RootTree",
"Launch" : [
{
"DisplayName" : "Clients",
"Launch" : [
{
"DisplayName" : "Clients Details",
"Launch" : [
{
"DisplayName" : "Item1",
"URI" : "/rest/..."
},
{
"DisplayName" : "Item2",
"URI" : "/rest/..."
},
{
"DisplayName" : "Item3",
"URI" : "/rest/..."
}
]
}
]
}
]
}
Here my structure, I used a class because of the recursive usage:
final class Url: Codable {
let name : String
let uri: String?
let launch: [LaunchStructure]?
enum CodingKeys: String, CodingKey {
case name = "DisplayName"
case uri = "URI"
case launch = "Launch"
}
}
final class LaunchStructure: Codable {
let launch: [Url]
enum CodingKeys: String, CodingKey {
case launch = "Launch"
}
}
In the Title and Subtitle I am not interested, therefore I have excluded it from the class. I would like to get the Displayname and the uri from the items. As I said, the structure is more nested, but always the same structure. Is it possible to read the elements using a recursive way.
I am to decode it in this manner:
...
let result = Result { try JSONDecoder().decode(LaunchStructure.self, from: data) }
Thank you, best regards
Arnold
You don't need two types here at all, just one will do:
struct Item: Codable {
let name : String? // not all entries in your example has it, so it's optional
let uri: String?
let launch: [Item]? // same here, all leaf items doesn't have it
enum CodingKeys: String, CodingKey {
case name = "DisplayName"
case uri = "URI"
case launch = "Launch"
}
}

Retrieving Keys From GeoFire within Radius in Swift

I have the following Firebase Database JSON data structure:
{
"Post List" : {
"-KUlvg8mCEGfY5ZSKCou" : {
"addedByUser" : "7llQbPdy2NV7aO337h7ap0qLOhC3",
"content" : "Post1",
"cost" : "$450",
"duration" : "Daily",
"latitude" : "25.0879721592719",
"longitude" : "55.1487715855458",
"number" : "01234567890",
"timestamp" : "Posted on: Sun 23 Oct"
}
},
"Post Locations" : {
"-KUlvg8mCEGfY5ZSKCou" : {
".priority" : "thrnwznh58",
"g" : "thrnwznh58",
"l" : [ 25.0879721592719, 55.1487715855458 ]
}
},
"User Locations" : {
"7llQbPdy2NV7aO337h7ap0qLOhC3" : {
".priority" : "thrnwypqu9",
"g" : "thrnwypqu9",
"l" : [ 25.0829547120321, 55.1505315855337 ]
}
}
}
I am looking to have a GeoFire query which will return all the keys [FIRAutoID's] within a radius of 1KM, for e.g. of "User Locations" these Posts are static and once the user location is set it will also be fixed. From the radius query result I am expecting to return "-KUlvg8mCEGfY5ZSKCou" so I can then make a reference the Post Details. Hope this makes sense. Many thanks, D. Cant find much online about GeoFire... looks like its still in the initial stages?...
Figured it out
func geoFireQuery() {
let circleQuery = geoFire.query(at: self.myLocation, withRadius: 0.5)
_ = circleQuery!.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in
print (key!)
})
circleQuery?.observeReady({
print("All initial data has been loaded and events have been fired!")
})
}
this seems to be giving me what I need. Now to reference those keys to the other part of the FBDB. :)

Are there different solution how retrieve waypoints?

"-KHbCuQflKHrJiUmfpG0" : {
"waypoints" : {
"-KHbCuQflKHrJiUmfpG1" : {
"latitude" : 13.17078652595298,
"longitude" : -59.5775944578738
},
"-KHbCuQflKHrJiUmfpG2" : {
"latitude" : 13.15541190861343,
"longitude" : -59.57619643155932
},
"-KHbCuQg9W_tebl1pU66" : {
"latitude" : 13.148444967591,
"longitude" : -59.5589266947333
}
},
"subtitle" : "jamesrick",
"title" : "Highway",
"type" : "polyline"
},
I have this structure for lines in Firebase. How retrieve all data with also nested node waypoints?
ref.observeEventType(.Value, withBlock: { polylines in
if let objects = polylines.children.allObjects as? [FDataSnapshot] {
for object in objects {
polylineDictionary = object.values as? Dictionary<String, AnyObjects> {
// ? ? ?
}
}
}
})
Now I have access to title, subtitle, type but how obtain access to waypoints? When I use
`polylineDictionary["waypoints"] as? [String: [String:Double]]`
so this dictionaries are not ordered. Thanks for some advice.
This is how I would get the nested waypoints... it's basically by iterating through the keys... I would also add some error checking when grabbing the longitude and latitudes to make sure they're there, but this is the gist:
if let polylineDictionary["waypoints"] as? [String: [String:Double]] {
let waypoints = Array(polylineDictionary.keys)
for i in 0..<waypoints.count {
let waypointId = waypoints[i]
let latitude = polylineDictionary[waypointId]["latitude"]
let longitutde = polylineDictionary[waypointId]["longitude"]
}
}
It's not clear from the question about ordering; if you just want to get to the waypoints, it's pretty straight forward:
Assume your complete Firebase structure is:
root_node
"-KHbCuQflKHrJiUmfpG0" : {
"waypoints" : {
"-KHbCuQflKHrJiUmfpG1" : {
"latitude" : 13.17078652595298,
"longitude" : -59.5775944578738
},
"-KHbCuQflKHrJiUmfpG2" : {
"latitude" : 13.15541190861343,
"longitude" : -59.57619643155932
},
"-KHbCuQg9W_tebl1pU66" : {
"latitude" : 13.148444967591,
"longitude" : -59.5589266947333
}
},
"subtitle" : "jamesrick",
"title" : "Highway",
"type" : "polyline"
}
Suppose I want the data for this specific node, -KHbCuQflKHrJiUmfpG0
let nodeRef = rootRef.childByAppendingPath("-KHbCuQflKHrJiUmfpG0")
nodeRef.observeSingleEventOfType(.Value , withBlock: { snapshot in
print(snapshot.value) //prints everything in the node
let title = snapshot.value["title"] as! String //to get any element
print(title) //prints Highway
var waypoints = [String: [String:String]]() //dict to hold key:value, unordered
waypoints = snapshot.value["waypoints"] as! Dictionary
print(waypoints) //prints the 3 waypoints and their children (as a dict)
//get fancy and convert the dictionary to an array (which can be sorted)
let arr = waypoints.map {"\($0) \($1)"}
for point in arr {
print(point)
}
}

Json results to a string array in swift

Hello i've been trying to make a json request and some of its results i want to put it to an array of string.
So i have the following code
var arrRes = [[String:AnyObject]]()
var nameaRR = [String]()
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, "https://graph.facebook.com/search", parameters: ["q": "", "type": "place", "center": "37.928319,23.7036673", "distance": "10000","limit": "1000", "access_token": "SomeToken", "expires_in": "5184000"])
.responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
//print(swiftyJsonVar)
if let resData = swiftyJsonVar["data"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
self.nameaRR = swiftyJsonVar["data"]["name"] as! [String]
print(self.nameaRR)
}
if self.arrRes.count > 0 {
self.kati.reloadData()
}
}
}
}
The JSON Resul is the following
{
"data" : [
{
"category_list" : [
{
"id" : "272705352802676",
"name" : "Outdoors"
},
{
"id" : "115725465228008",
"name" : "Region"
}
],
"id" : "552889064768971",
"name" : "Παλαιο Φαληρο", //This String i want to put in an Array
"category" : "Local business",
"location" : {
"street" : "",
"city" : "Palaión Fáliron",
"country" : "Greece",
"longitude" : 23.6944070162,
"zip" : "17562",
"latitude" : 37.9284637008,
"state" : ""
}
}
]
}
I get a warning
Cast from 'JSON' to unrelated type '[String]' always fails
But i'm stuck of how can i put all the Strngs to the array nameaRR.
Can anyone help me find my mistake? Thanks!
look do like that
if let resData = swiftyJsonVar["data"] as? [[String:AnyObject]] {
if let categorylist = resData["category_list"] as? [[String:AnyObject]]{
if let id = categorylist["id"] as? Int{
print(id)
}
}
}

how manipulate a NSDictionary generated by a json file in swift

I've a NSDictionary populated by a JSON file.
JSON file content (initially)
{
"length" : 0,
"locations" : []
}
I want add some elements in "locations". The elements have the below structure:
[
"name" : "some_name",
"lat" : "4.88889",
"long" : "5.456789",
"date" : "19/01/2015"
]
In next code I read de JSON File
let contentFile = NSData(contentsOfFile: pathToTheFile)
let jsonDict = NSJSONSerialization.JSONObjectWithData(contentFile!, options: nil, error: &writeError) as NSDictionary`
like you can see jsonDict contain the JSON's info but in a NSDictionary object.
At this point I can't add the elements mentioned before, I tried insert NSData, NSArray, Strings, and nothing results for me
After do this I want convert "final" NSDictionary in JSON again to save it in a file.
The "final" NSDictionary must be like this
{
"length" : 3,
"locations" : [
{
"name" : "some_name",
"lat" : "4.88889",
"long" : "5.456789",
"date" : "19/01/2015"
},
{
"name" : "some_name_2",
"lat" : "8.88889",
"long" : "9.456789",
"date" : "19/01/2015"
},
{
"name" : "some_name_3",
"lat" : "67.88889",
"long" : "5.456789",
"date" : "19/01/2015"
}
]
}
"length" control the index for new element
I have no more ideas to do this. thanks in advance
If you want to be able to modify the dictionary, you can make it mutable:
let jsonDict = NSJSONSerialization.JSONObjectWithData(contentFile!, options: .MutableContainers, error: &writeError) as NSMutableDictionary
The resulting NSMutableDictionary can be modified. For example:
let originalJSON = "{\"length\" : 0,\"locations\" : []}"
let data = originalJSON.dataUsingEncoding(NSUTF8StringEncoding)
var parseError: NSError?
let locationDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers, error: &parseError) as NSMutableDictionary
locationDictionary["length"] = 1 // change the `length` value
let location1 = [ // create dictionary that we'll insert
"name" : "some_name",
"lat" : "4.88889",
"long" : "5.456789",
"date" : "19/01/2015"
]
if let locations = locationDictionary["locations"] as? NSMutableArray {
locations.addObject(location1) // add the location to the array of locations
}
If you now constructed JSON from the updated locationDictionary, it would look like:
{
"length" : 1,
"locations" : [
{
"long" : "5.456789",
"lat" : "4.88889",
"date" : "19/01/2015",
"name" : "some_name"
}
]
}