我正在尝试添加一个表格查看从google places API获取的用户评论
"reviews" : [
{
"author_name" : "Robert Ardill",
"author_url" : "https://www.google.com/maps/contrib/106422854611155436041/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-T47KxWuAoJU/AAAAAAAAAAI/AAAAAAAAAZo/BDmyI12BZAs/s128-c0x00000000-cc-rp-mo-ba1/photo.jpg",
"rating" : 5,
"relative_time_description" : "a month ago",
"text" : "Awesome offices. Great facilities, location and views. Staff are great hosts",
"time" : 1491144016
}
],
https://developers.google.com/places/web-service/details,现在我试图只添加评级,author_name和文字,这是我的自定义tableViewCell
import UIKit
class myReviewTableCell: UITableViewCell {
@IBOutlet weak var revText: UITextView!
@IBOutlet weak var revRating: UIImageView!
@IBOutlet weak var revAuthor: UILabel!
var place: EClass?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func revSelect(place:Eclass) {
NearbyPlaces.getPlaceDetails(place: place) { (place) in
DispatchQueue.main.async {
if let rev = place.details?["reviews"] as? [String:Any] {
}
}
}
}
}
我需要正确构建func revSelect(place:Eclass)
并在我的cellForRowAt中调用它之后。我已经尝试添加其他参数,如
if (place.details?["website"] as? String) != nil {
self.webButton.isHidden = false
}
else {
self.webButton.isHidden = true
}
if let phoneNumber = place.details?["international_phone_number"] as? String {
self.myLabel4.text = "\(phoneNumber)"
}
else {
self.myLabel4.isHidden = true
}
}
}
并且它们运行良好,但是评论是一个数组,所以不同,我也试图遵循参数的逻辑" geometry"
if let g = placeInfo["geometry"] as? [String:Any] {
if let l = g["location"] as? [String:Double] {
if let lat = l["lat"], let lng = l["lng"] {
location = CLLocationCoordinate2D.init(latitude: lat, longitude: lng)
}
}
}
我在另一个课程中添加了,但它没有用。所以我必须在我的func revSelect中做什么来制作rev.Author.text =" author_name" ,revText =" text"等?? (我之前已经问过这个,但我无法解决问题)
// UPDATE
这是我用来获取地点详情的功能
static func getPlaceDetails(place:EClass, completion: @escaping (EClass) -> Void) {
guard place.details == nil else {
completion(place)
return
}
var params : [String : Any]
params = [
"key" : AppDelegate.googlePlacesAPIKey,
"placeid" : place.placeId,
]
Alamofire.request(googlePlaceDetailsHost, parameters: params, encoding: URLEncoding(destination: .queryString)).responseJSON { response in
let value = response.result.value as? [String : Any]
place.details = (value)?["result"] as? [String : Any]
/* print(((value)?["result"] as? [String : Any] ?? [String : Any]()).debugDescription) */
completion(place)
}
}
答案 0 :(得分:0)
您只需要在Swift中研究JSON解析。这个article将是一个很好的阅读。
一旦理解了JSON的结构,就很容易阅读。您只需要正确映射每种类型。 Swift 4引入了Codable协议,当与JSONDecoder结合使用时,可以更好地解析和使用这些数据。
struct Response: Codable {
var reviews: [Review]
}
struct Review: Codable {
var authorName: String
var authorUrl: String
var rating: Int
var text: String
enum CodingKeys: String, CodingKey {
case authorName = "author_name"
case authorUrl = "author_url"
case rating
case text
}
}
let data = """
{
"reviews" : [{
"author_name" : "Robert Ardill",
"author_url" : "https://www.google.com/maps/contrib/106422854611155436041/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-T47KxWuAoJU/AAAAAAAAAAI/AAAAAAAAAZo/BDmyI12BZAs/s128-c0x00000000-cc-rp-mo-ba1/photo.jpg",
"rating" : 5,
"relative_time_description" : "a month ago",
"text" : "Awesome offices. Great facilities, location and views. Staff are great hosts",
"time" : 1491144016
}]
}
""".data(using: .utf8)
guard let data = data else { throw NSError() }
let response = try? JSONDecoder().decode(Response.self, from: data)
let reviews = response?.reviews // array of reviews
let review = reviews?.first // first one from the list (if any)
print(review?.authorName)
print(review?.rating)
因此,使用上面的示例,我创建了一个Review类型,并将此类型的属性映射到JSON响应中的类型。然后我们可以使用JSONDecoder为我们完成剩下的工作。
输出:
可选(" Robert Ardill")
可选(5)
更新:
您需要执行类似的操作,但您可能需要根据响应值更改结构结构。
Alamofire.request(googlePlaceDetailsHost, parameters: params, encoding: URLEncoding(destination: .queryString)).responseJSON { response in
guard let data = response.data else { throw NSError() }
let response = try? JSONDecoder().decode(Response.self, from: data)
let reviews = response?.reviews // array of reviews
completion(reviews?.first)
}
答案 1 :(得分:0)
您可以使用object mapper,并且有关于github的文档