我试图在swift中解析JSON。在我的程序早期,当我说
时,它的工作正常func connectionDidFinishLoading(connection: NSURLConnection!) {
var err: NSError
// throwing an error on the line below (can't figure out where the error message is)
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
println(jsonResult.valueForKey("results")?.valueForKey("geometry")?.valueForKey("location")?.valueForKey("lat"))
if(jsonResult.valueForKey("status") as String == "OK"){
var nearestLat = jsonResult.valueForKey("results")?.valueForKey("geometry")?.valueForKey("location")?.valueForKey("lat") as [CLLocationDegrees]
var nearestLong = jsonResult.valueForKey("results")?.valueForKey("geometry")?.valueForKey("location")?.valueForKey("lng") as [CLLocationDegrees]
var openStatus = jsonResult.valueForKey("results")?.valueForKey("opening_hours")?.valueForKey("open_now") as [Int]
var address = jsonResult.valueForKey("results")?.valueForKey("vicinity") as [String]
var name = jsonResult.valueForKey("results")?.valueForKey("name") as [String]
var boolOpen = Bool(openStatus[0])
var nearestChipotle = NearestRestaurant(name: name[0], lat: nearestLat[0], long: nearestLong[0], address: address[0], open: boolOpen)
println(nearestChipotle.milesToRestuarant(self.latitude, fromLong: self.longitude))
//
// println(nearestLat[0])
// println(nearestLong[0])
// println(boolOpen)
// println(address[0])
// println(name[0])
}
else if jsonResult.valueForKey("status") as String == "ZERO_RESULTS"{
println("NO CHIPOTLE!")
}
不能工作
if(jsonResult.valueForKey("status") as String == "OK"){
println(jsonResult.valueForKey("rows")?.valueForKey("elements")?.valueForKey("distance")?.valueForKey("text") as String)
//var metersTo = jsonResult.valueForKey("rows")?.valueForKey("elements")?.valueForKey("distance")?.valueForKey("value") as String
//println(metersTo)
//self.distanceTo = metersToMiles()
//println(self.distanceTo)
//self.timeTo = jsonResult.valueForKey("rows")?.valueForKey("elements")?.valueForKey("duration")?.valueForKey("text") as String
//println(metersTo)
//println(self.timeTo)
}
else if jsonResult.valueForKey("status") as String == "ZERO_RESULTS"{
println("NO DISTANCE COMPUTED!")
}
但是当我稍后在程序中尝试相同的方法时,我收到此错误
由此代码
引起这是我的输出的样子。由于额外的'('')'看起来我赢得的第二个JSON没有被正确播放。括弧。
处理这些括号或导致它们的原因是什么?
这两个JSON信息都来自Google API