2017-07-24 114 views
0

如何将JSON对象转换为CLLocationDegrees?如何将JSON对象转换为CLLOCATIONDEGREES?

if let value = response.result.value { 

     let json = JSON(value) 

     for (key,subJson):(String, JSON) in json { 

      let lat = subJson["latitude"] as! CLLocationDegrees 
      let lng = subJson["longitude"] as! CLLocationDegrees // This will return error 



     } 

    } 

为JSON数据类型是

{ 
    "latitude": 2323.44555, 
    "longitude": 2313.344555 
} 

它只是将不会从JSON工作

+0

正好返回什么错误? – Larme

+0

@Larme'从JSON投射到不相关的对象CLLocationDegrees(又名Double)总是失败',那么应用程序崩溃 – sinusGob

+0

错字'经度'vs'经度'? – vadian

回答

3

该错误消息

角色无关对象CLLocationDegrees(又名双人间)总是失败

很清楚,下标JSONSwiftyJSON库返回JSON。你必须得到doubleValueCLLocationDegreesDouble一个类型别名)

for (key,subJson) in json { // don't annotate types unless the compiler tells you 
     let lat = subJson["latitude"].doubleValue 
     let lng = subJson["longitude"].doubleValue 
     // do something with lat and lng 
} 
+0

非常感谢!它有助于。 – sinusGob

0

这是你可以得到它

let json = [ 
    "latitude": 2323.44555, 
    "longitude": 2313.344555 
] 
var location = CLLocationCoordinate2D() 
if let lat = json["latitude"] as? Double,let lng = json["longitude"] as? Double { 
     location.latitude = lat 
     location.longitude = lng 
}