在下面的方法中,我可以获取位置值而不是位置值。我怎么能得到这个位置?
提前谢谢!!
func searchDB(looking: String){
var urlString:String = "URLGOESHERE?q=\(looking)"
let url = NSURL(string: urlString)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if error != nil {
println(error)
}
else {
//processing data
if let arr = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? [AnyObject] {
for currPlace in arr {
println(currPlace["name"])
println(currPlace["location"])
}
}
else {
errorOccurred = true
}
}//eo potential data
})
task.resume()
}//eom
这是我得到的结果输出:
Optional(Buddha-Bar)
Optional(nil)
JSON示例:
样本数据:
{
“formatted_address”:“8-12 Rue Boissy d'Anglas,75008 Paris,France”,
“几何”:{
“位置”:{
“lat”:48.868194,
“lng”:2.321596
}
},
“icon”:“http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png”,
“id”:“560dd225114fd10997f75ee777bad84bcb40c529”,
“名字”:“佛陀吧”,
“opening_hours”:{
“open_now”:是的,
“weekday_text”:[]
},
“照片”:[
{
“身高”:848,
“html_attributions”:[],
“photo_reference”: “CnRnAAAAifUh9MiqwAgQYdwEp-EnS4e_nPQN_mPYIqdI49UKun_CZKxgtUh_ZqT8QBEqBuel9seoZvyyIVvA5-TlweEqO9_2tORg_cmTi_Cy5L_PAthdZd1_Krqbf7oJNy81RWD3brA8fzeIKJfQTMgo-AT19RIQAg5kKSqeoeedm69uhUWKvBoULDJ1-PoSgv4Lsg5y1rjU_pHm_Ng”
“宽度”:1919年
}
],
“place_id”:“ChIJRS81ac1v5kcRRUqQBmTTJJU”,
“price_level”:3,
“评级”:3.7,
“参考”: “CmReAAAAjJskNN69nw3gBVtqLpsX11Psr-QvK6cHPLhF-oDXAbYq7dwLn65b1svUJOLVnRgAbg4K3w7qCj9_hkXvx20q4YNR2714ZQQw89GyFGCtXAxonRh09_uvgK97DewsYRyUEhAczR_GzOvU0mmG1OZr0X3kGhQeJ1Vr3RSnI6VXyzh83W_LIcUK_g”,
“类型”:[
“酒吧”,
“餐厅”,
“食物”,
“建立”
]
},
没有空格的Json数据
样本数据: { “formatted_address”:“8-12 Rue Boissy d'Anglas,75008 Paris,France”, “几何”:{ “地点”: { “lat”:48.868194, “lng”:2.321596 } }, “icon”:“http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png”, “id”:“560dd225114fd10997f75ee777bad84bcb40c529”, “名字”:“佛陀吧”, “营业时间”: { “open_now”:是的, “weekday_text”:[] }, “相片”: [ { “身高”:848, “html_attributions”:[], “photo_reference”: “CnRnAAAAifUh9MiqwAgQYdwEp-EnS4e_nPQN_mPYIqdI49UKun_CZKxgtUh_ZqT8QBEqBuel9seoZvyyIVvA5-TlweEqO9_2tORg_cmTi_Cy5L_PAthdZd1_Krqbf7oJNy81RWD3brA8fzeIKJfQTMgo-AT19RIQAg5kKSqeoeedm69uhUWKvBoULDJ1-PoSgv4Lsg5y1rjU_pHm_Ng” “宽度”:1919年 } ] “place_id”:“ChIJRS81ac1v5kcRRUqQBmTTJJU”, “price_level”:3, “评级”:3.7, “参考”: “CmReAAAAjJskNN69nw3gBVtqLpsX11Psr-QvK6cHPLhF-oDXAbYq7dwLn65b1svUJOLVnRgAbg4K3w7qCj9_hkXvx20q4YNR2714ZQQw89GyFGCtXAxonRh09_uvgK97DewsYRyUEhAczR_GzOvU0mmG1OZr0X3kGhQeJ1Vr3RSnI6VXyzh83W_LIcUK_g”, “类型”:[ “酒吧”, “餐厅”, “餐饮”, “建立” ] },
答案 0 :(得分:0)
为数据的相关部分添加一些格式:
sample data: { "formatted_address": "8-12 Rue Boissy d'Anglas, 75008 Paris, France", "geometry": { "location": { "lat": 48.868194, "lng": 2.321596 } }, "icon": "http://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png", "id": "560dd225114fd10997f75ee777bad84bcb40c529", "name": "Buddha-Bar",
目前还不清楚“样本数据”是什么意思,因为它没有被引用,它可能是print语句添加的东西(我的猜测),在这种情况下不需要访问组件。
名称的地址为:
[“name”]
位置在lat / lon,因此将有两次访问:
[ “几何”] [ “位置”] [ “LAT”]
[ “几何”] [ “位置”] [ “LON”]
在上面,必须应用适用的语言语法,在Swift中会有一些痛苦。
有关JSON的信息,请参阅json.org。
答案 1 :(得分:0)
func searchDB(looking: String){
var errorOccurred:Bool = false
var urlString:String = "URLGOESHERE?q=\(looking)"
let url = NSURL(string: urlString)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if error != nil {
println(error)
errorOccurred = true
} else {
// println(response) //response from post
//processing data
let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil)
if let statusesArray = jsonObject as? NSArray{
println("********* LEVEL 1 *******")
println(statusesArray[0])
if let aStatus = statusesArray[0] as? NSDictionary{
println("********* LEVEL 2 *******")
println(aStatus)
if let geometry = aStatus["geometry"] as? NSDictionary{
println("********* LEVEL 3 *******")
println(geometry)
if let currLocation = geometry["location"] as? NSDictionary{
println("********* LEVEL 4 *******")
println(currLocation)
println(currLocation["lat"])
println(currLocation["lng"])
}
}
}
}
else {
errorOccurred = true
}
}//eo potential data
})
task.resume()
}//eom