0

I need to access lat and lng. How can I do that?

enter image description here

This is what I do now, and it doesn't work:

latitude <- map["location"]["lat"]
longitude <- map["location"]["lng"]
1
  • Why you don't use the new Codable protocol ? Commented Mar 29, 2018 at 12:56

4 Answers 4

3

You just simply need:

latitude <- map["location.lat"]
longitude <- map["location.lng"]

So far ObjectMapper supports dot notation within keys for easy mapping of nested objects (you could find it under "Easy Mapping of Nested Objects" in library documentation).

Sign up to request clarification or add additional context in comments.

Comments

2

You would need to declare a separated class for the location to contain the lat and the lng properties:

class Location: Mappable {
    var lat: Double?
    var lng: Double?

    required init?(map: Map){ }

    func mapping(map: Map) {
        lat <- map["lat"]
        lng <- map["lng"]
    }
}

thus you could use it as:

class Base: Mappable {
    var location: Location?
    // ...

    required init?(map: Map){ }

    func mapping(map: Map) {
        location <- map["location"]
        //...
    }
}

It would represent a nested type for mapping the base object.


Aside bar note: you might also want to check Codable - Encoding and Decoding Custom Types.

4 Comments

Are you sure that your answer worked? :D
The same question about first element in array of elements. Do you know how to do this?
@BartłomiejSemańczyk could you please elaborate?
I have found an answer: ‚.0’
1

Instead of using ObjectMapper , you can use JSONDecoder , that will enable you to write only the key of the objects inside the json , instead of writing them twice with ObjectMapper. (as a property and inside map function ) , especially if the server keys naming is suitable for you

1 Comment

Yes, you are right, but very often I need to change their naming;) But your answer is very helpful...
-3

Hope this helps you

 let data = NSData(contentsOf: searchUrl as URL)
                let json = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:AnyObject]
                let status = json["status"] as! String
                print(status)
                if status == "OK" {
                    if let result = json["results"] as? [[String:AnyObject]] {
                        if let geometry = result[0]["geometry"] as? [String:AnyObject] {
                            if let location = geometry["location"] as? [String:AnyObject] {
                                lat = location["lat"] as! Double
                                lng = location["lng"] as! Double
                                print(lat)
                                print(lng)
                            }
                      }
                 }

3 Comments

The question has nothing to do with using JSONSerialization. Even so, your answer doesn't provide a correct result.
but am using this concept in my project
anyways it's not helping you!.But definitely it's help some developers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.