ios - 使用 Swift 访问 JSON

标签 ios json swift

我目前正在开发一个应用程序,我想显示有关特定地点的一些信息。我正在使用 Google Places API 来执行此操作,并尝试从此 JSON 响应中提取一些信息:

{
"html_attributions": [],
"result": {
"address_components": [],
"adr_address": "5, \u003cspan class=\"street-address\"\u003e48 Pirrama Rd\u003c/span\u003e, \u003cspan class=\"locality\"\u003ePyrmont\u003c/span\u003e \u003cspan class=\"region\"\u003eNSW\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e2009\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",
"formatted_address": "5, 48 Pirrama Rd, Pyrmont NSW 2009, Australia",
"formatted_phone_number": "(02) 9374 4000",
"geometry": {},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id": "4f89212bf76dde31f092cfc14d7506555d85b5c7",
"international_phone_number": "+61 2 9374 4000",
"name": "Google",
"photos": [],
"place_id": "ChIJN1t_tDeuEmsRUsoyG83frY4",
"rating": 4.5,
"reference": "CmRaAAAAVvEJLOszIgZMqrn59xg_wEmLJUpC52Zd7HZzzcv0OsaRobY_f8galxBVNsyEgC9nhUsI7BQQcTYCA0t_f8JlhEV-dftt-OhapkRRvQ12g6R3FU-d6OWB-T1vYVIcRuEBEhBha4swICH7pUUsjBRivnHTGhT1Y97NAmr0iWe4qffGJH0iY96GKg",
"reviews": [],
"scope": "GOOGLE",
"types": [
  "point_of_interest",
  "establishment"
],
"url": "https://maps.google.com/?cid=10281119596374313554",
"user_ratings_total": 133,
"utc_offset": 660,
"vicinity": "5 48 Pirrama Road, Pyrmont",
"website": "https://www.google.com.au/about/careers/locations/sydney/"
},
"status": "OK"
}

我在 View Controller 中使用的代码是:

let reposURL = NSURL(string: "https://maps.googleapis.com/maps/api/place/details/json?placeid=\(chosenPlaceID!)&key=SOMEAPIKEY")!

    if let JSONData = NSData(contentsOfURL: reposURL) {

        if let json = (try? NSJSONSerialization.JSONObjectWithData(JSONData, options: [])) as? NSDictionary {

            if let reposArray = json["result"] as? [NSDictionary] {

                for item in reposArray {
                    placeRepositories.append(PlaceRepository(json: item ))
                }

            }
        }
    }

我的 placeRepository Controller 中的代码是:

class PlaceRepository {

var name: String?
var formattedAddress: String?
var formattedPhoneNumber: String?
var internationPhoneNumber: String?
var types: [String]?

init(json: NSDictionary) {
    self.name = json["name"] as? String
    self.formattedAddress = json["formatted_address"] as? String
    self.formattedPhoneNumber = json["formatted_phone_number"] as? String
    self.internationPhoneNumber = json["international_phone_number"] as? String
    self.types = json["types"] as? [String]

    }
}

我已经放置了一个断点来尝试找出发生了什么,并且 reposArray 代码中的 for 项目从未被访问,它在前面的 if 语句中被跳过,但我不确定为什么?任何帮助将不胜感激。

谢谢。

最佳答案

您的回复表明 json["result"] 不是字典数组 ... [NSDictionary] ...其 NSDictionary

更改此行

if let reposArray = json["result"] as? [NSDictionary] { ... }

if let reposArray = json["result"] as? NSDictionary {
      PlaceRepository(json: reposArray )
}   

After that you can't loop NSDictionary like
for item in reposArray { .. }

关于ios - 使用 Swift 访问 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35813694/

相关文章:

ios - Swift:将 View 从堆栈 View 带到前面

ios - 关闭 View Controller 意外行为

iphone - 如何将数组元素添加到已包含另一个数组值的 NSMutableArray?

javascript - 如何显示 Bitly API 调用的短 URL?

ios - 过滤器 UISearchController 之后的 didSelectRowAtIndexPath indexpath - Swift

php - 通过 SQL 的多维 JSON 对象

php - 在 json 模式数据中包含 php 变量 - WordPress 站点 - 无效的 json 错误

swift - ApplicationDidEnterBackground : NSTimer 时意外崩溃

ios - 如何取消 iOS 中的完成处理程序

ios - 具有相同捆绑 ID 但来自不同项目的应用程序不会更新