gpt4 book ai didi

json - 如何快速从 "https://maps.googleapis.com/maps/api/place/nearbysearch/json? "获取 JSON 到数组

转载 作者:搜寻专家 更新时间:2023-11-01 07:07:50 25 4
gpt4 key购买 nike

我正在尝试从“https://maps.googleapis.com/maps/api/place/nearbysearch/json?”获取 JSON 到(swift 3)带有数组的 tableView Controller 。但是从 google api 返回的数据是字典和放在 tableView 上的错误我应该如何处理我的代码?

任何答案都会有帮助

这是我的代码:

    var listData : [[String : AnyObject]] = [[String : AnyObject]]()

override func viewDidLoad() {
super.viewDidLoad()

self.tableView.dataSource = self
self.tableView.delegate = self

let myURL : URL = URL(string: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=13.88068,100.43575&radius=300&type=gas_station&key=YOURKEY")!

Alamofire.request(myURL).responseJSON(completionHandler: {
response in
switch response.result{
case .success:

self.listData = response.result.value as! [[String : AnyObject]]

print(self.listData.description)

self.tableView.reloadData()

case .failure(let error):
print(error)
}

})

最佳答案

您正在将结果转换为一个数组(更准确地说,是一个字典数组)。但响应不是数组。它是一本字典。但是与其 results 键关联的值一个字典数组:

var listData = [[String: Any]]()

override func viewDidLoad() {
super.viewDidLoad()

tableView.dataSource = self
tableView.delegate = self

let url = URL(string: "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=13.88068,100.43575&radius=300&type=gas_station&key=YOURKEY")!

Alamofire.request(url).responseJSON { response in
switch response.result {
case .success(let value):
guard let json = value as? [String: Any], let listData = json["results"] as? [[String: Any]] else {
print("Response does not contain results")
return
}

self.listData = listData

print(listData)

self.tableView.reloadData()

case .failure(let error):
print(error)
}
}
}

关于json - 如何快速从 "https://maps.googleapis.com/maps/api/place/nearbysearch/json? "获取 JSON 到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47015725/

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