Analyzing json data from google maps api swift

advertisements

I was in the process of parsing JSON data from the google maps API to get my current location city using longitude & latitude.

But now im stuck with my code here and i cannot access the data in the results. For instance i want to get the data in the "administrative_area_level_2" for the key "long name". How can i access it?

dispatch_async(dispatch_get_main_queue(), {

            if let urlContent = data{
                do{
                    let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
                    if let results = jsonResult["results"] as? [[String : AnyObject]] {

                        for result in results{

                            if let addressComponents = result["address_components"] as? [[String : AnyObject]] {

                                print(addressComponents)
                            }
                        }
                    }
                  //  print(jsonResult)

                } catch{

                }

            }

        })

Here are the addressComponents JSON results in console:

    [["types": (
    route
), "short_name": Abou Al MAGD Al Askalani, "long_name": Abou Al MAGD Al Askalani], ["types": (
    "administrative_area_level_3",
    political
), "short_name": Al Golf, "long_name": Al Golf], ["types": (
    "administrative_area_level_2",
    political
), "short_name": Nasr City, "long_name": Nasr City], ["types": (
    "administrative_area_level_1",
    political
), "short_name": Cairo Governorate, "long_name": Cairo Governorate], ["types": (
    country,
    political
), "short_name": EG, "long_name": Egypt]]

Here are the results jsonResult results in console:

results =     (
            {
        "address_components" =             (
                            {
                "long_name" = "Abou Al MAGD Al Askalani";
                "short_name" = "Abou Al MAGD Al Askalani";
                types =                     (
                    route
                );
            },
                            {
                "long_name" = "Al Golf";
                "short_name" = "Al Golf";
                types =                     (
                    "administrative_area_level_3",
                    political
                );
            },
                            {
                "long_name" = "Nasr City";
                "short_name" = "Nasr City";
                types =                     (
                    "administrative_area_level_2",
                    political
                );
            },
                            {
                "long_name" = "Cairo Governorate";
                "short_name" = "Cairo Governorate";
                types =                     (
                    "administrative_area_level_1",
                    political
                );
            },
                            {
                "long_name" = Egypt;
                "short_name" = EG;
                types =                     (
                    country,
                    political
                );
            }
        );


For deeply nested JSON I'd recommend a third-party library like SwiftyJSON but this is a "without" solution. It uses the filter function to get the item containing the requested type

if let urlContent = data {
  do {
    let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions()) as! [String : AnyObject]

    if let results = jsonResult["results"] as? [[String : AnyObject]] {
      for result in results{
        if let addressComponents = result["address_components"] as? [[String : AnyObject]] {

          let filteredItems = addressComponents.filter{ if let types = $0["types"] as? [String] {
            return types.contains("administrative_area_level_2") } else { return false } }
          if !filteredItems.isEmpty {
             print(filteredItems[0]["long_name"] as! String)
          }
        }
      }
    }
  } catch let error as NSError {
    print(error)
  }
}