我正在尝试通过网络解析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
来做到这一点
答案 0 :(得分:2)
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!)
答案 1 :(得分:0)
根对象是具有String
键和Bookings
值的字典-请使用开头的大写字母命名结构。
所以你必须解码
let loginDetails = try decoder.decode([String:Bookings].self, from: response.data!)