gpt4 book ai didi

ios - Alamofire 在 swift 5 中响应无效的 json

转载 作者:行者123 更新时间:2023-11-29 11:25:19 25 4
gpt4 key购买 nike

我正在使用 Swift 5 和 Alamofire 5,但响应是无效的 JSON。我调用 Postman,响应 JSON 是:

[ {
"location_name": "Hamdard(waqf) Limited, Pantha Path, ধানমন্ডি আ/এ, লালমাটিয়া, ঢাকা, ঢাকা বিভাগ, বাংলাদেশ",
"description": "D M-Th 16-0299",
"engine": 1
{
"location_name": "602/Chha, Red Crescent Shorak, Mogbazar, ঢাকা, ঢাকা বিভাগ, বাংলাদেশ",
"description": "D M-Th 16-1953",
"engine": 1
} ]

我这样调用 Alamofire:

 let headers: HTTPHeaders = [
"Content-Type": "application/json",
"YumaSession": "my session_id"
]

AF.request(url,method:.get,encoding: JSONEncoding.default, headers: headers).responseJSON { response in

switch response.result {
case .success(let response):

print("response--",response)

case .failure(let error):

print("error--->",error)
}

但是 Alamofire 会像这样返回带有 Unicode 的 JSON 响应:

 ({
description = "D M-Th 16-0299";
"location_name" = "357 Modubag,Mogbazar, \U09a2\U09be\U0995\U09be, \U09a2\U09be\U0995\U09be \U09ac\U09bf\U09ad\U09be\U0997, \U09ac\U09be\U0982\U09b2\U09be\U09a6\U09c7\U09b6";
lon = "90.41140444444444";
online = 1;

},
{
description = "D M-Th 16-1953";
"location_name" = "53, Gulshan, \U0997\U09c1\U09b2\U09b6\U09be\U09a8 \U09e7, \U09a2\U09be\U0995\U09be, \U09a2\U09be\U0995\U09be \U09ac\U09bf\U09ad\U09be\U0997, 1212, \U09ac\U09be\U0982\U09b2\U09be\U09a6\U09c7\U09b6";
lon = "90.40848888888888";
online = 1;

} )

请帮助我获得正确的 JSON 响应。

最佳答案

顺便说一句,这很简单。您必须遵循 Codable 方法。这是我的答案。希望它能帮助你。我创建了本地 json 文件,您可以相应地传递 url。

您的 JSON:您缺少一个大括号和逗号。

[ {
"location_name": "Hamdard(waqf) Limited, Pantha Path, ধানমন্ডি আ/এ, লালমাটিয়া, ঢাকা, ঢাকা বিভাগ, বাংলাদেশ",
"description": "D M-Th 16-0299",
"engine": 1

},
{
"location_name": "602/Chha, Red Crescent Shorak, Mogbazar, ঢাকা, ঢাকা বিভাগ, বাংলাদেশ",
"description": "D M-Th 16-1953",
"engine": 1
} ]

型号:

struct DataClass : Codable {

let descriptionField : String?
let engine : Int?
let locationName : String?

enum CodingKeys: String, CodingKey {
case descriptionField = "description"
case engine = "engine"
case locationName = "location_name"
}

init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
descriptionField = try values.decodeIfPresent(String.self, forKey: .descriptionField)
engine = try values.decodeIfPresent(Int.self, forKey: .engine)
locationName = try values.decodeIfPresent(String.self, forKey: .locationName)
}

}

代码:

var AppData = [DataClass]()

override func viewDidLoad() {
super.viewDidLoad()


if let url = Bundle.main.url(forResource: "file", withExtension: "json") {
do {

let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let jsonData = try decoder.decode(Array<DataClass>.self, from: data)
self.AppData = jsonData
print (jsonData.first?.locationName ?? "")

} catch {
print("error:\(error)")
}
}
}

打印:

Hamdard(waqf) Limited, Pantha Path, ধানমন্ডি আ/এ, লালমাটিয়া, ঢাকা, ঢাকা বিভাগ, বাংলাদেশ

关于ios - Alamofire 在 swift 5 中响应无效的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59168402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com