Alamofire无法读取JSON文件

时间:2018-12-15 07:06:19

标签: iphone swift

尝试构建一个ios应用,该应用将找到最近的餐厅并将其放置在Google地图上。但无法读取JSON文件。

var coordinate = [AnyObject]()

Alamofire.request(key).responseJSON { (response) in
        let data = response.result

        if let json =  data.value as? Dictionary<String,AnyObject>{
            if let results = json["results"]
            {
                if let geometry = results["geometry"] as? Dictionary<String,AnyObject>
                {
                    if let location = geometry["location"]
                    {
                        self.coordinate = location as! [AnyObject]
                                                }
                }
            }

        }
    }

当我将结果存储在数组中时,上面的代码不起作用。我想从JSON文件中读取几何。该怎么办 下面是JSON文件格式

{
html_attributions: [ ],
next_page_token: "CrQCIwEAAD1hVtsv2Rq1gXGAFL67ucrXNKqh-2Km4rUSgkPUYZA4apoWTxWmUgVKX9tYTyOkHRtfU6hJsu7QBq1fZNDWB-bAcryr8ADW4bBfszYCIDkg9F2JBrxn2iMFYspvx0aQSyblSD3EoYP2qmjD4itG_pCC67Eo_Pk1Xcsoxx4ZW5O9wvtrby7WNPOUGnIlSBOZ4R-5tjwkOEZCcroM3lBjF1fQbrZ7F-2_ymgf-h3C1sdW4iV4AG85rgIcWeeAW_uwamnyvChTSqSjf_q4IiqNoA9T-q4ZmZGXgkIOk2omzjqEm1Q563RRjGXSrRV8kTeF4t_S-Xeq4-05SRwe3Gx-3t9KBDXR9W9nRH8W08H08wrBYs0O239HUXLbgGLgmZr6h8n4YQbJETLCwI_QTCtOh3sSEMCf6iNSUBjd76aHUHN1b6YaFHTkB0-5Yh8szipA_BLiVYIE0ebm",
results: [
{
geometry: {
location: {
lat: 51.51021369999999,
lng: -0.1315736
},
viewport: {
northeast: {
lat: 51.51158128029149,
lng: -0.130123169708498
},
southwest: {
lat: 51.5088833197085,
lng: -0.132821130291502
}
}
},
icon: "https://maps.gstatic.com/mapfiles/place_api/icons/lodging- 
71.png",
id: "b501c78eff5bb3e4b7f67dfe97727cec52a693c5",
name: "Thistle Piccadilly Hotel",
photos: [
{
height: 337,
html_attributions: [
"<a href="https://maps.google.com/maps/contrib/110195987647911662808/photos">Thistle Piccadilly Hotel</a>"
],
photo_reference: "CmRaAAAA5ohge6XYb1kjWk5C_KJdzFRx3SBF9QjkZUgUQ36av_eSP-9sjkvwncvOk5gPvVeC_Tgj4lcYr0DDm-x3JxMvnLMykI7BzAJAdoYs2VKeGNPWxX0VKdH2m72PSUScH_f4EhBJqRr0iXUwsO-IQHO2TWPGGhS8mbZaVg5BaJLnGAtE6t-cyi2naA",
width: 338
}
],
place_id: "ChIJZewrLtIEdkgRtl2nZKboAgk",
plus_code: {
compound_code: "GV69+39 St. James's, London, United Kingdom",
 global_code: "9C3XGV69+39"
},
rating: 4.4,
reference: "ChIJZewrLtIEdkgRtl2nZKboAgk",
scope: "GOOGLE",
types: [
"bar",
"lodging",
"restaurant",
"point_of_interest",
"food",
"establishment"
],
vicinity: "Coventry Street, London"
}
],
status: "OK"
}

如何从上述JSON文件读取纬度和经度。

2 个答案:

答案 0 :(得分:0)

首先,Swift 3+中的JSON值永远不会foreach(DatagridViewRow dr in datagridview) { sql = "update table1 set name=@name where id=@id; update table1 set name=@name where id=@id; update table1 set name=@name where id=@id;" } ,它是AnyObject

名称Any隐含,并且JSON证明了这一点–请注意results –该值是数组,而不是字典

[]

答案 1 :(得分:0)

您可以使用SwiftyJSON。解析JSON数据更容易。

    var coordinate = [AnyObject]()

    Alamofire.request(key).responseJSON { (response) in
        let data = response.result
        let json = try JSON(data: data)
        let results = json["results"].array
        for result in results{
            let location = result["geometry"]["location"]
            coordinates.append((location["lat"], location["lng"]))

        }
    }