gpt4 book ai didi

json - 解码 JSON 时出错 - keyNotFound(CodingKeys

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

我正在尝试通过网络解析 JSON 数据。下面你可以看到魔法发生的地方。

func getBookingsForDate(date: String, completionHandler: @escaping ([String:String]) -> Void ){

struct bookings: Codable {

var bookieName : String
var bookieNumber: String
var booked: String
var bookingTime: String

private enum Codingkeys: String, CodingKey{

case bookieName
case bookieNumber
case booked
case bookingTime
}
}

let params = ["date":date]

let urlString = "http://mscissorss.pythonanywhere.com/getBookings/"
Alamofire.request(urlString, method: .get, parameters: params).responseJSON {
response in
switch response.result {
case .success(let JSON):

let decoder = JSONDecoder()
guard let _ = response.data else{
return
}
do {
let loginDetails = try decoder.decode(bookings.self, from: response.data!)
print(loginDetails)
} catch let err{
print(err)
}

//let bookings = JSON as! NSDictionary
//completionHandler(JSON)
/*
do {
let decoder = JSONDecoder()
let gitData = try decoder.decode(bookings.self, from: JSON)
print(gitData.bookieName)

} catch let err {
print("Err", err)
}
*/
break
case .failure(let error):
print(error)
}
}
}

根据代码,我收到以下错误消息:

keyNotFound(CodingKeys(stringValue: "bookieName", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"bookieName\", intValue: nil) (\"bookieName\").", underlyingError: nil))

我得到的 JSON 响应如下所示:

{
0 = {
booked = false;
bookieName = "";
bookieNumber = "";
bookingTime = "10:00";
};
1 = {
booked = false;
bookieName = "";
bookieNumber = "";
bookingTime = "10:30";
};
10 = {
booked = false;
bookieName = "";
bookieNumber = "";
bookingTime = "15:00";
};
11 = {
booked = false;
bookieName = "";
bookieNumber = "";
bookingTime = "15:30";
};
12 = {
booked = false;
bookieName = "";
bookieNumber = "";
bookingTime = "16:00";
};
13 = {
booked = false;
bookieName = "";
bookieNumber = "";
bookingTime = "16:30";
};
14 = {
booked = false;
bookieName = "";
bookieNumber = "";
bookingTime = "17:00";
};
15 = {
booked = false;
bookieName = "";
bookieNumber = "";
bookingTime = "17:30";
};
16 = {
booked = false;
bookieName = "";
bookieNumber = "";
bookingTime = "18:00";
};
2 = {
booked = false;
bookieName = "";
bookieNumber = "";
bookingTime = "11:00";
};
}

这是我第一次解码,所以如果你有答案,请尝试解释一下为什么我需要做出我需要的改变。

更新

将代码更改为@sh_khan 和@vadian 建议的代码后,它可以解析它,但是我在解析的对象中仍然遇到此错误:

["1": MagicS.(unknown context at 0x106932738).bookings(bookieName: "", bookieNumber: "", booked: "false", bookingTime: "10:30"), 
"0": MagicS.(unknown context at 0x106932738).bookings(bookieName: "", bookieNumber: "", booked: "false", bookingTime: "10:00"),

此外,如果我希望能够访问单个值,可以说第一个带有键 "0" -> bookieName 的项目。 ,我将如何使用 loginDetails 来做到这一点?

最佳答案

第一个 booked 是一个 Bool,如果您不重命名键,则不需要 private enum Codingkeys

struct Booking: Codable {

let bookieName : String
let bookieNumber: String
let booked: Bool
let bookingTime: String

}

第二次这样解码

let loginDetails = try decoder.decode([String:Booking].self, from: response.data!)

关于json - 解码 JSON 时出错 - keyNotFound(CodingKeys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53377296/

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