Search code examples
iosiphoneswiftipadnsurl

Get value from server reponse swift


I am new to Swift, I want to get value of latitude and longitde from server reponse.

How do i get the value?

Here is my code:

WebServiceHelper.sharedInstance.makeWebServiceCall(urlAddress: "http://jsonplaceholder.typicode.com/users/1", requestMethod: "GET", params: ["Key" : "value"], Success: { (JSON:Any) in

            print("Success Received data is: \(JSON)")

        }, Error: { (JSON:Any) in

            print("Error Received data is: \(JSON)")

        }) { (JSON:Any) in

            print("Header Received data is: \(JSON)")
        }

I am able to get server reponse Here it is:

Success Received data is: {
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "[email protected]",
  "address": {
    "street": "Kulas Light",
    "suite": "Apt. 556",
    "city": "Gwenborough",
    "zipcode": "92998-3874",
    "geo": {
      "latitude": "-37.3159",
      "longitude": "81.1496"
    }
  },
  "phone": "1-770-736-8031 x56442",
  "website": "hildegard.org",
  "company": {
    "name": "Romaguera-Crona",
    "catchPhrase": "Multi-layered client-server neural-net",
    "bs": "harness real-time e-markets"
  }
}

How do i get?


Solution

  • you can try like this to get lat long :

    if let result = JSON as? [String:AnyObject]{
            let address = result?["address"] as? [String:AnyObject]
            let geo = address?["geo"] as? [String:AnyObject]
            print(geo?["lng"])
            print(geo?["lat"]) }