如何修复 json 使用 combine 解析期间描述的错误?
How do I fix the error described during json parsing using combine?
我正在尝试使用组合框架解析来自网站 alphavantage.com 的股票数据。尽管我的数据模型具有与 json 匹配的正确值,但我一直收到此 error Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"bestMatches\", intValue: nil) (\"bestMatches\").", underlyingError: nil))
。我该如何解决这个问题?
struct SearchResults: Decodable{
let bestMatches : [SearchResult]
enum CodingKeys: String, CodingKey{
case bestMatches = "bestMatches"
}
}
struct SearchResult : Decodable{
let symbol : String?
let name : String?
let type : String?
let currency :String?
enum CodingKeys:String, CodingKey{
case symbol = "1. symbol"
case name = "2. name"
case type = "3. type"
case currency = "8. currency"
}
}
struct APIservice{
let apiKey = "U893NJLDIREGERHB"
func fetchSymbols(keyword:String)-> AnyPublisher<SearchResults,Error>{
let urlSTring = "https://www.alphavantage.co/query?function=\(keyword)H&keywords=tesco&apikey=U893NJLDIREGERHB"
let url = URL(string: urlSTring)!
return URLSession.shared.dataTaskPublisher(for: url)
.map({[=11=].data})
.decode(type: SearchResults.self, decoder: JSONDecoder())
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}
}
func performSearch(){
apiSerivice.fetchSymbols(keyword: "S&P500").sink { (completion) in
switch completion {
case .failure(let error):
print(error)
case . finished:
break
}
} receiveValue: { (SearchResults) in
print(SearchResults.bestMatches)
}.store(in: &subcribers)
您的查询不正确。查看 symbol search 的文档,您的 URL 必须为 function
查询传入 SYMBOL_SEARCH
。
您对关键字的查询也未按应有的方式 URL 编码,因此 "S&P 500"
存在在插入字符串时创建无效查询的问题。更好的方法是使用 URLComponents
这样可以安全地为您处理。
代码:
func fetchSymbols(keyword: String) -> AnyPublisher<SearchResults, Error> {
guard var components = URLComponents(string: "https://www.alphavantage.co/query") else {
return Fail(
outputType: SearchResults.self,
failure: APIError.invalidComponents
).eraseToAnyPublisher()
}
components.queryItems = [
URLQueryItem(name: "function", value: "SYMBOL_SEARCH"),
URLQueryItem(name: "keywords", value: keyword),
URLQueryItem(name: "apikey", value: apiKey)
]
guard let url = components.url else {
return Fail(
outputType: SearchResults.self,
failure: APIError.invalidURL
).eraseToAnyPublisher()
}
return URLSession.shared.dataTaskPublisher(for: url)
.map(\.data)
.decode(type: SearchResults.self, decoder: JSONDecoder())
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}
enum APIError: Error {
case invalidComponents
case invalidURL
}
我将查询从 "S&P500"
更改为 "S&P 500"
,否则没有结果。您也可以删除 SearchResults
中多余的 CodingKeys
,因为那没有效果。
注意:不要暴露您的 API 密钥!
我正在尝试使用组合框架解析来自网站 alphavantage.com 的股票数据。尽管我的数据模型具有与 json 匹配的正确值,但我一直收到此 error Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"bestMatches\", intValue: nil) (\"bestMatches\").", underlyingError: nil))
。我该如何解决这个问题?
struct SearchResults: Decodable{
let bestMatches : [SearchResult]
enum CodingKeys: String, CodingKey{
case bestMatches = "bestMatches"
}
}
struct SearchResult : Decodable{
let symbol : String?
let name : String?
let type : String?
let currency :String?
enum CodingKeys:String, CodingKey{
case symbol = "1. symbol"
case name = "2. name"
case type = "3. type"
case currency = "8. currency"
}
}
struct APIservice{
let apiKey = "U893NJLDIREGERHB"
func fetchSymbols(keyword:String)-> AnyPublisher<SearchResults,Error>{
let urlSTring = "https://www.alphavantage.co/query?function=\(keyword)H&keywords=tesco&apikey=U893NJLDIREGERHB"
let url = URL(string: urlSTring)!
return URLSession.shared.dataTaskPublisher(for: url)
.map({[=11=].data})
.decode(type: SearchResults.self, decoder: JSONDecoder())
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}
}
func performSearch(){
apiSerivice.fetchSymbols(keyword: "S&P500").sink { (completion) in
switch completion {
case .failure(let error):
print(error)
case . finished:
break
}
} receiveValue: { (SearchResults) in
print(SearchResults.bestMatches)
}.store(in: &subcribers)
您的查询不正确。查看 symbol search 的文档,您的 URL 必须为 function
查询传入 SYMBOL_SEARCH
。
您对关键字的查询也未按应有的方式 URL 编码,因此 "S&P 500"
存在在插入字符串时创建无效查询的问题。更好的方法是使用 URLComponents
这样可以安全地为您处理。
代码:
func fetchSymbols(keyword: String) -> AnyPublisher<SearchResults, Error> {
guard var components = URLComponents(string: "https://www.alphavantage.co/query") else {
return Fail(
outputType: SearchResults.self,
failure: APIError.invalidComponents
).eraseToAnyPublisher()
}
components.queryItems = [
URLQueryItem(name: "function", value: "SYMBOL_SEARCH"),
URLQueryItem(name: "keywords", value: keyword),
URLQueryItem(name: "apikey", value: apiKey)
]
guard let url = components.url else {
return Fail(
outputType: SearchResults.self,
failure: APIError.invalidURL
).eraseToAnyPublisher()
}
return URLSession.shared.dataTaskPublisher(for: url)
.map(\.data)
.decode(type: SearchResults.self, decoder: JSONDecoder())
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}
enum APIError: Error {
case invalidComponents
case invalidURL
}
我将查询从 "S&P500"
更改为 "S&P 500"
,否则没有结果。您也可以删除 SearchResults
中多余的 CodingKeys
,因为那没有效果。
注意:不要暴露您的 API 密钥!