无法解码get请求中带有文本的json



我需要创建这样的GET请求:

https://public-api.nazk.gov.ua/v1/declaration/?q=完成

https://public-api.nazk.gov.ua/v1/declaration/?q=В

=后的最后一个字符是西里尔字母符号

我提出这样的获取请求:

var hostURL = "https://public-api.nazk.gov.ua/v1/declaration/?q="
hostURL = hostURL + searchConditions
let escapedSearchConditions = hostURL.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)
let url = URL(string: escapedSearchConditions!)!

请求是:https://public-api.nazk.gov.ua/v1/declaration/?q=%D0%9F%D1%80%D0%BE

从服务器返回必要的数据,但返回的数据无法解码
它适用于搜索条件下的整数,但不适用于西里尔文(

import Foundation
struct Declarant: Codable {
var id: String
var firstname: String
var lastname: String
var placeOfWork: String
var position: String
var linkPDF: String
}
struct DeclarationInfo: Codable {
let items: [Declarant]
}

导入基础

struct DeclarationInfoController {
func fetchDeclarationInfo (with searchConditions: String, completion: @escaping(DeclarationInfo?) -> Void) {
var hostURL = "https://public-api.nazk.gov.ua/v1/declaration/?q="
hostURL = hostURL + searchConditions
let escapedSearchConditions = hostURL.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)
let url = URL(string: escapedSearchConditions!)!
print(url)
let dataTask = URLSession.shared.dataTask(with: url) {
(data, response, error) in
let jsonDecoder = JSONDecoder()
print("Trying to decode data...")
if let data = data,
let declarationInfo = try? jsonDecoder.decode(DeclarationInfo.self, from: data) {
completion(declarationInfo)
print(declarationInfo)
} else {
print("Either no data was returned, or data was not properly decoded.")
completion(nil)
}
}
dataTask.resume()
}

}

import UIKit
class DeclarationViewController: UIViewController {
let declarationInfoController = DeclarationInfoController()
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var resultLabel: UILabel!

@IBAction func beginSearchButton(_ sender: UIButton) {
declarationInfoController.fetchDeclarationInfo(with: searchBar.text!) { (declarationInfo) in
if let declarationInfo = declarationInfo {
DispatchQueue.main.async {
self.resultLabel.text = declarationInfo.items[0].lastname
}
}
}
}

}

永远不要在解码JSON时忽略错误使用try?Codable错误具有令人难以置信的描述性,可以准确地告诉你哪里出了问题。

始终使用类似的do catch

do {
let declarationInfo = try jsonDecoder.decode(DeclarationInfo.self, from: data)
} catch { print error }

并且打印CCD_ 4而不是无用的文字串。


该错误与西里尔文无关。

在您之前的一个问题的评论中建议的JSON结构

struct Item: Codable {
let id, firstname, lastname, placeOfWork: String
let position, linkPDF: String
}

揭示错误(强调最重要的部分(

keyNotFound(编码键(字符串值:"position">,intValue:nil(,Swift.DecodingError.Context(编码路径:[CodingKeys(字符串值:"items">(,intValue:nil(,_JSONKey,debugDescription:">没有与键CodingKeys关联的值(字符串值:\"position\">,intValue:nil((\"position \"(.",underlyingError:nil(

它清楚地描述了在结构体Item中,数组索引11处的项中没有键position的值。

解决方案是将这个特定的结构成员声明为可选的

struct Item: Codable {
let id, firstname, lastname, placeOfWork: String
let position : String?
let linkPDF: String
}

再次:不要忽略错误,它们可以帮助您立即解决问题

更新

if let data = data,
let declarationInfo = try? jsonDecoder.decode(DeclarationInfo.self, from: data) {
completion(declarationInfo)
print(declarationInfo)
} else {
print("Either no data was returned, or data was not properly decoded.")
completion(nil)
}

通过

do {
if let data = data {
let declarationInfo = try jsonDecoder.decode(DeclarationInfo.self, from: data) 
completion(declarationInfo)
print(declarationInfo)
return
} catch {
print(error) 
}
completion(nil)

你会打印错误,你会知道解码失败的原因吗

最新更新