>问题
我试图使用以下网址获取评论数据和电话号码,但除了电话号码和评论的text
和rating
之外,大多数值都是零。但是,如果您直接在网络上点击 URL,则可以检查预期的 json 数据。https://maps.googleapis.com/maps/api/place/details/json?place_id="PLACE ID"&fields=reviews,formatted_phone_number&key="api key"
参考资料说(如果可用(。 在缺失数据描述的末尾。这是什么意思?我不知道这是什么意思。
我认为没有语法错误,因为可以获得数据。 此外,由于模型通过使用快速类型直接点击 API 生成了确认的数据,因此很难想到拼写错误。
struct Empty: Codable {
let result: Result
let status: String?
}
// MARK:-Result
struct Result: Codable {
let formattedPhoneNumber: String?
let reviews: [Review]
}
// MARK:-Review
struct Review: Codable {
let authorName: String?
let authorurl: String?
let language: String?
let profilePhotourl: String?
let rating: Int?
let relativeTimeDescription, text: String?
let time: Int?
}
jsonData (我想知道我是否可以按原样显示数据,所以我用 x 替换实际数据(
{
"html_attributions" :[],
"result" :{
"formatted_phone_number": "xx-xxxx-xxxx",
"reviews" :[
{
"author_name" :"xxxx",
"author_url": "https://www.google.com/maps/xxxxx/xxxxxxx/reviews",
"language" :"xx",
"profile_photo_url" :"https://xxxxx.com/-xxxxxxx/xxxxxx/xxxxxx/xxxxxxx/xxxxxx/photo.jpg",
"rating" :4
"relative_time_description": "x months ago",
"text": "xxxxxxxxxxxxxx.",
"time": 1234567
},
{
"author_name": "xxxxxxxx",
"author_url": "https://www.google.com/maps/contrib/xxxxxxxx/reviews",
"language" :"xx",
"profile_photo_url" :"https://lh6.ggpht.com/-xxxxxxx/xxxxxxxx/xxxxxxxxx/xxxxxxxx/xxxxxxx/photo.jpg",
"rating" :5
"relative_time_description": "x weeks ago",
"text": "xxxxxxxxx",
"time": 1234567
}
]
},
"status": "OK"
}
服务器响应采用蛇形大小写,因此您需要在解码 JSON 之前设置解码器密钥编码策略。像这样的东西
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase