gpt4 book ai didi

ios - 使用 Swift 访问 JSON

转载 作者:行者123 更新时间:2023-11-30 13:38:45 26 4
gpt4 key购买 nike

我目前正在开发一个应用程序,我想显示有关特定地点的一些信息。我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com