<分区>
我有一个 api 响应片段,看起来像这样......
{
"status": "OK",
"predictions": [
{
"description": "Hilton Head Island, SC, USA",
"id": "67bd386c0fb3d4f77bf3cb283a6d75565ea11732",
"matched_substrings": [
{
"length": 6,
"offset": 0
}
],
"place_id": "ChIJrRnTjtx5_IgRPSii63qm5Sw",
"reference": "ChIJrRnTjtx5_IgRPSii63qm5Sw",
"structured_formatting": {
"main_text": "Hilton Head Island",
"main_text_matched_substrings": [
{
"length": 6,
"offset": 0
}
],
"secondary_text": "SC, USA"
},
"types": [
"locality",
"political",
"geocode"
]
}
}
从这里我想要 main_text
和 secondary_text
。为此,我制作了一个模型类......
class FilteredLocations: Codable {
var locationDetails: [String: Any]
enum CodingKeys: String, CodingKey {
case locationDetails = "structured_formatting"
}
required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
locationDetails = try? values.decode([String: Any].self, forKey: .locationDetails)//ERROR HERE
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(locationDetails, forKey: .locationDetails)//ERROR HERE
}
}
但在上面的代码中,在提到的地方 //ERROR HERE
会抛出错误 Reference to member 'locationDetails' cannot be resolved without a contextual type
我做错了什么......?
编辑 1:这就是我解析数据的方式..
if let productService = result?["predictions"] as? [[String: Any]] ,
let jsonData = try? JSONSerialization.data(withJSONObject: productService as Any, options: []) {
do {
let filteredLocations = try? JSONDecoder().decode(FilteredLocations.self, from: jsonData)
self.tableview.reloadData()
} catch {
print("error \(error.localizedDescription)")
}
}
编辑 2 谷歌 API 响应:
{
"predictions" : [
{
"description" : "Hilton Head Island, SC, USA",
"id" : "gfhgj3hgjhsgvh4hj4hgj424hfjhjha11732",
"matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"place_id" : "dfgegregergrgrergergewrer5Sw",
"reference" : "bvEQfdgetehetheterbgcbng5Sw",
"structured_formatting" : {
"main_text" : "Hilton Head Island",
"main_text_matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"secondary_text" : "SC, USA"
},
"terms" : [
{
"offset" : 0,
"value" : "Hilton Head Island"
},
{
"offset" : 20,
"value" : "SC"
},
{
"offset" : 24,
"value" : "USA"
}
],
"types" : [ "locality", "political", "geocode" ]
}
],
"status" : "OK"
}
编辑 3 我如何进行 api 调用和获取数据以进行解析..
WebServiceClient.shared.getNearbyLocationsSearchList(withParameters: parameters, inputText: searchText) { [weak self] (isSuccess, result) in
guard let `self` = self else { return }
if isSuccess, result != nil {
print("Successfully fetched nearby locations!")
if let productService = result?["predictions"] as? [[String: Any]] ,
let jsonData = try? JSONSerialization.data(withJSONObject: productService as Any, options: []) {
do {
let filteredLocations = try? JSONDecoder().decode([FilteredLocations].self, from: jsonData)
} catch {
print("error \(error.localizedDescription)")
}
}
}
编辑 4 这是调用网络服务后来自 api 的结果
..
{
description = "Houston, TX, USA";
id = 25faf0a7ef1056b980f3a19237cfa8e295668123;
"matched_substrings" = (
{
length = 1;
offset = 0;
}
);
"place_id" = ChIJAYWNSLS4QIYROwVl894CDco;
reference = ChIJAYWNSLS4QIYROwVl894CDco;
"structured_formatting" = {
"main_text" = Houston;
"main_text_matched_substrings" = (
{
length = 1;
offset = 0;
}
);
"secondary_text" = "TX, USA";
};
terms = (
{
offset = 0;
value = Houston;
},
{
offset = 9;
value = TX;
},
{
offset = 13;
value = USA;
}
);
types = (
locality,
political,
geocode
);
}
编辑 5