【问题标题】:parse google places json in swift快速解析谷歌地方json
【发布时间】:2014-11-18 01:15:54
【问题描述】:

我不明白如何解决这个该死的问题......

我正在尝试解析来自 google 地方调用的一些 json。

这是代码中有趣的部分:

// Sending Asynchronous request using NSURLConnection
        NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{(response:NSURLResponse!, responseData:NSData!, error: NSError!)->Void in

            if error != nil
            {
                println(error.description)
                println("Couldn't reach api")
            }
            else
            {
                //Converting data to String
                var responseDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData,options: NSJSONReadingOptions.MutableContainers, error:nil) as NSDictionary
                println("SUCCESS")
                var results:NSArray = responseDict["results"] as NSArray
                var item:NSDictionary = results[0] as NSDictionary
                self.googleAddress = item["formatted_address"] as NSString

                println("accessing geometry")
                let googleGeo:NSArray = item["geometry"] as NSArray
                println("accessing location")
                let googleLoc:NSDictionary = googleGeo[0] as NSDictionary
                        println("grabbing lat")
                        self.googleLatitude = googleLoc["lat"] as NSString
                        println("grabbing lng")
                        self.googleLongitude = googleLoc["lng"] as NSString
                println(self.googleLatitude)

项目如下所示:

"formatted_address" : "5035 Rue Saint-Denis, Montréal, Québec H2J 2L8, Canada",
         "geometry" : {
            "location" : {
               "lat" : 45.526682,
               "lng" : -73.588599
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
         "id" : "11b53fd7721df9a1becb825ab8df3e91d8985624",
         "name" : "La Petite Marche",
         "opening_hours" : {
            "open_now" : true,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 960,
               "html_attributions" : [
                  "\u003ca href=\"https://plus.google.com/110862802692218089410\"\u003eÉric Paris\u003c/a\u003e"
               ],
               "photo_reference" : "CnRoAAAAHPehKz876gJCu1lmLr-hJkFYGgGKeGKzwbjiAYYFt3INTfN1fLUwEWG4WK5g4qWf9bL1GjxWTI4saFh2pYFYOMgZymVFRRwZGcuqzNTNMnzeZw_HS_fhXkkdQje7P3jS-n69c0L-agGyAIVpjMSTqBIQNgTb589QgVo135Ycvoy1choUKv7swtNzbdrDNJayJEqM8-I6sjA",
               "width" : 1280
            }
         ],
         "place_id" : "ChIJi-DyptcbyUwRiWA4C6Bjrf0",
         "rating" : 3.5,
         "reference" : "CoQBcwAAAIXlYOLFA_7Puiw4TYEt2GuMQAY9K2tykoBCEwjqVNIREoBwC9awyriE2zfe3vHzjKpHEXs_8Vdq3Ca0-nVR84lA-GubyXh-IDYAb9zZZE7bPi3TNVlNhjrLMRyLDMEOJdZEekkaFK51Kir0ZZ_-E-bx8ErrtV--eplGnmcV9GmtEhCfDLFPRnUSTHrPzY_pHqrdGhSMLgdiCK6iGAqEAT55jdk9vx31wg",
         "types" : [ "cafe", "restaurant", "food", "establishment" ]

正如您在我的代码中看到的,我想从 json 访问 lat 和 lng 值。

不幸的是,我找不到访问它的方法,并且应用程序在第一行崩溃:

let googleGeo:NSArray = item["geometry"] as NSArray

有人能解释一下在 Swift 中解析 json 是如何工作的吗?我想避免使用外部库(用于学习目的)

谢谢!

【问题讨论】:

    标签: json parsing swift


    【解决方案1】:
    let googleGeo:NSArray = item["geometry"] as NSArray
    

    问题是您将geometry 转换为一个数组,而它实际上是另一个字典(至少根据您发布的 JSON)。相反,您应该这样做:

    let googleGeo = item["geometry"] as NSDictionary
    let googleLoc = googleGeo["location"] as NSDictionary
    let latitude = googleLoc["lat"] as Float
    let longitude = googleLoc["long"] as Float
    

    【讨论】:

    • 别担心!在 Swift 中处理 JSON 尤其复杂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    相关资源
    最近更新 更多