从服务器响应swift获取价值

时间:2017-08-09 07:34:53

标签: ios iphone swift ipad nsurl

我是Swift的新手, 我想从服务器响应中获得纬度和longitde的值。

我如何获得价值?

这是我的代码:

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)")
        }

我能得到服务器响应这里是:

Success Received data is: {
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "Sincere@april.biz",
  "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"
  }
}

我如何获得?

1 个答案:

答案 0 :(得分:1)

你可以尝试这样做得很长:

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"]) }