Error when decoding JSON - keyNotFound(CodingKeys
up vote
0
down vote
favorite
I am trying to parse JSON data over the network. Below you can see where the magic is happening.
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)
}
}
}
Given the code i am getting the following error message:
keyNotFound(CodingKeys(stringValue: "bookieName", intValue: nil), Swift.DecodingError.Context(codingPath: , debugDescription: "No value associated with key CodingKeys(stringValue: "bookieName", intValue: nil) ("bookieName").", underlyingError: nil))
And the JSON response that i am getting looks like this:
{
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";
};
}
It is the first time i am decoding so if you have an answer please try to explain a bit to why i need to make the change that i need.
UPDATE
After changing the code to what @sh_khan and @vadian suggested it worked to parse it however i am still getting this error inside my parsed object:
["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"),
Also, if i want to be able to access a single value lets say the first item with the key "0" -> bookieName
, how would i do that using loginDetails
json swift decode
add a comment |
up vote
0
down vote
favorite
I am trying to parse JSON data over the network. Below you can see where the magic is happening.
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)
}
}
}
Given the code i am getting the following error message:
keyNotFound(CodingKeys(stringValue: "bookieName", intValue: nil), Swift.DecodingError.Context(codingPath: , debugDescription: "No value associated with key CodingKeys(stringValue: "bookieName", intValue: nil) ("bookieName").", underlyingError: nil))
And the JSON response that i am getting looks like this:
{
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";
};
}
It is the first time i am decoding so if you have an answer please try to explain a bit to why i need to make the change that i need.
UPDATE
After changing the code to what @sh_khan and @vadian suggested it worked to parse it however i am still getting this error inside my parsed object:
["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"),
Also, if i want to be able to access a single value lets say the first item with the key "0" -> bookieName
, how would i do that using loginDetails
json swift decode
1
You aren't managing the first level with is the "11" key (for instance, or all the other ones in fact). What you can decode with your current code is something like that:{"booked":false,"bookieName":"","bookieNumber":"","bookingTime":"10:00"}
.
– Larme
Nov 19 at 15:03
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to parse JSON data over the network. Below you can see where the magic is happening.
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)
}
}
}
Given the code i am getting the following error message:
keyNotFound(CodingKeys(stringValue: "bookieName", intValue: nil), Swift.DecodingError.Context(codingPath: , debugDescription: "No value associated with key CodingKeys(stringValue: "bookieName", intValue: nil) ("bookieName").", underlyingError: nil))
And the JSON response that i am getting looks like this:
{
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";
};
}
It is the first time i am decoding so if you have an answer please try to explain a bit to why i need to make the change that i need.
UPDATE
After changing the code to what @sh_khan and @vadian suggested it worked to parse it however i am still getting this error inside my parsed object:
["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"),
Also, if i want to be able to access a single value lets say the first item with the key "0" -> bookieName
, how would i do that using loginDetails
json swift decode
I am trying to parse JSON data over the network. Below you can see where the magic is happening.
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)
}
}
}
Given the code i am getting the following error message:
keyNotFound(CodingKeys(stringValue: "bookieName", intValue: nil), Swift.DecodingError.Context(codingPath: , debugDescription: "No value associated with key CodingKeys(stringValue: "bookieName", intValue: nil) ("bookieName").", underlyingError: nil))
And the JSON response that i am getting looks like this:
{
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";
};
}
It is the first time i am decoding so if you have an answer please try to explain a bit to why i need to make the change that i need.
UPDATE
After changing the code to what @sh_khan and @vadian suggested it worked to parse it however i am still getting this error inside my parsed object:
["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"),
Also, if i want to be able to access a single value lets say the first item with the key "0" -> bookieName
, how would i do that using loginDetails
json swift decode
json swift decode
edited Nov 19 at 15:30
asked Nov 19 at 14:58
EmbeddedOS
195
195
1
You aren't managing the first level with is the "11" key (for instance, or all the other ones in fact). What you can decode with your current code is something like that:{"booked":false,"bookieName":"","bookieNumber":"","bookingTime":"10:00"}
.
– Larme
Nov 19 at 15:03
add a comment |
1
You aren't managing the first level with is the "11" key (for instance, or all the other ones in fact). What you can decode with your current code is something like that:{"booked":false,"bookieName":"","bookieNumber":"","bookingTime":"10:00"}
.
– Larme
Nov 19 at 15:03
1
1
You aren't managing the first level with is the "11" key (for instance, or all the other ones in fact). What you can decode with your current code is something like that:
{"booked":false,"bookieName":"","bookieNumber":"","bookingTime":"10:00"}
.– Larme
Nov 19 at 15:03
You aren't managing the first level with is the "11" key (for instance, or all the other ones in fact). What you can decode with your current code is something like that:
{"booked":false,"bookieName":"","bookieNumber":"","bookingTime":"10:00"}
.– Larme
Nov 19 at 15:03
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
Firs booked
is a Bool
and no need for private enum Codingkeys
if you won't rename the keys
struct Booking: Codable {
let bookieName : String
let bookieNumber: String
let booked: Bool
let bookingTime: String
}
Second decode like this
let loginDetails = try decoder.decode([String:Booking].self, from: response.data!)
please check update
– EmbeddedOS
Nov 19 at 15:30
The parse is successful context is where you write the class in
– Sh_Khan
Nov 19 at 15:35
I did not understand what you mean. Could you explain in another way? @sh_Khan
– EmbeddedOS
Nov 19 at 19:10
your context is the projectName.className
– Sh_Khan
Nov 19 at 21:13
add a comment |
up vote
0
down vote
The root object is a dictionary with String
keys and Bookings
values – please name structs with a starting capital letter.
So you have to decode
let loginDetails = try decoder.decode([String:Bookings].self, from: response.data!)
please check update
– EmbeddedOS
Nov 19 at 15:30
The unknown context is not related to the question about keyNotFound(CodingKeys. And what isMagicS
? To get the value for a specific key just writeloginDetails["0"]!.bookieName
– vadian
Nov 19 at 15:33
MagicS is the name of my swift project.Thank you for answering the other question @vadian
– EmbeddedOS
Nov 19 at 15:43
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Firs booked
is a Bool
and no need for private enum Codingkeys
if you won't rename the keys
struct Booking: Codable {
let bookieName : String
let bookieNumber: String
let booked: Bool
let bookingTime: String
}
Second decode like this
let loginDetails = try decoder.decode([String:Booking].self, from: response.data!)
please check update
– EmbeddedOS
Nov 19 at 15:30
The parse is successful context is where you write the class in
– Sh_Khan
Nov 19 at 15:35
I did not understand what you mean. Could you explain in another way? @sh_Khan
– EmbeddedOS
Nov 19 at 19:10
your context is the projectName.className
– Sh_Khan
Nov 19 at 21:13
add a comment |
up vote
2
down vote
Firs booked
is a Bool
and no need for private enum Codingkeys
if you won't rename the keys
struct Booking: Codable {
let bookieName : String
let bookieNumber: String
let booked: Bool
let bookingTime: String
}
Second decode like this
let loginDetails = try decoder.decode([String:Booking].self, from: response.data!)
please check update
– EmbeddedOS
Nov 19 at 15:30
The parse is successful context is where you write the class in
– Sh_Khan
Nov 19 at 15:35
I did not understand what you mean. Could you explain in another way? @sh_Khan
– EmbeddedOS
Nov 19 at 19:10
your context is the projectName.className
– Sh_Khan
Nov 19 at 21:13
add a comment |
up vote
2
down vote
up vote
2
down vote
Firs booked
is a Bool
and no need for private enum Codingkeys
if you won't rename the keys
struct Booking: Codable {
let bookieName : String
let bookieNumber: String
let booked: Bool
let bookingTime: String
}
Second decode like this
let loginDetails = try decoder.decode([String:Booking].self, from: response.data!)
Firs booked
is a Bool
and no need for private enum Codingkeys
if you won't rename the keys
struct Booking: Codable {
let bookieName : String
let bookieNumber: String
let booked: Bool
let bookingTime: String
}
Second decode like this
let loginDetails = try decoder.decode([String:Booking].self, from: response.data!)
answered Nov 19 at 15:08
Sh_Khan
34.9k51125
34.9k51125
please check update
– EmbeddedOS
Nov 19 at 15:30
The parse is successful context is where you write the class in
– Sh_Khan
Nov 19 at 15:35
I did not understand what you mean. Could you explain in another way? @sh_Khan
– EmbeddedOS
Nov 19 at 19:10
your context is the projectName.className
– Sh_Khan
Nov 19 at 21:13
add a comment |
please check update
– EmbeddedOS
Nov 19 at 15:30
The parse is successful context is where you write the class in
– Sh_Khan
Nov 19 at 15:35
I did not understand what you mean. Could you explain in another way? @sh_Khan
– EmbeddedOS
Nov 19 at 19:10
your context is the projectName.className
– Sh_Khan
Nov 19 at 21:13
please check update
– EmbeddedOS
Nov 19 at 15:30
please check update
– EmbeddedOS
Nov 19 at 15:30
The parse is successful context is where you write the class in
– Sh_Khan
Nov 19 at 15:35
The parse is successful context is where you write the class in
– Sh_Khan
Nov 19 at 15:35
I did not understand what you mean. Could you explain in another way? @sh_Khan
– EmbeddedOS
Nov 19 at 19:10
I did not understand what you mean. Could you explain in another way? @sh_Khan
– EmbeddedOS
Nov 19 at 19:10
your context is the projectName.className
– Sh_Khan
Nov 19 at 21:13
your context is the projectName.className
– Sh_Khan
Nov 19 at 21:13
add a comment |
up vote
0
down vote
The root object is a dictionary with String
keys and Bookings
values – please name structs with a starting capital letter.
So you have to decode
let loginDetails = try decoder.decode([String:Bookings].self, from: response.data!)
please check update
– EmbeddedOS
Nov 19 at 15:30
The unknown context is not related to the question about keyNotFound(CodingKeys. And what isMagicS
? To get the value for a specific key just writeloginDetails["0"]!.bookieName
– vadian
Nov 19 at 15:33
MagicS is the name of my swift project.Thank you for answering the other question @vadian
– EmbeddedOS
Nov 19 at 15:43
add a comment |
up vote
0
down vote
The root object is a dictionary with String
keys and Bookings
values – please name structs with a starting capital letter.
So you have to decode
let loginDetails = try decoder.decode([String:Bookings].self, from: response.data!)
please check update
– EmbeddedOS
Nov 19 at 15:30
The unknown context is not related to the question about keyNotFound(CodingKeys. And what isMagicS
? To get the value for a specific key just writeloginDetails["0"]!.bookieName
– vadian
Nov 19 at 15:33
MagicS is the name of my swift project.Thank you for answering the other question @vadian
– EmbeddedOS
Nov 19 at 15:43
add a comment |
up vote
0
down vote
up vote
0
down vote
The root object is a dictionary with String
keys and Bookings
values – please name structs with a starting capital letter.
So you have to decode
let loginDetails = try decoder.decode([String:Bookings].self, from: response.data!)
The root object is a dictionary with String
keys and Bookings
values – please name structs with a starting capital letter.
So you have to decode
let loginDetails = try decoder.decode([String:Bookings].self, from: response.data!)
answered Nov 19 at 15:03
vadian
138k13144165
138k13144165
please check update
– EmbeddedOS
Nov 19 at 15:30
The unknown context is not related to the question about keyNotFound(CodingKeys. And what isMagicS
? To get the value for a specific key just writeloginDetails["0"]!.bookieName
– vadian
Nov 19 at 15:33
MagicS is the name of my swift project.Thank you for answering the other question @vadian
– EmbeddedOS
Nov 19 at 15:43
add a comment |
please check update
– EmbeddedOS
Nov 19 at 15:30
The unknown context is not related to the question about keyNotFound(CodingKeys. And what isMagicS
? To get the value for a specific key just writeloginDetails["0"]!.bookieName
– vadian
Nov 19 at 15:33
MagicS is the name of my swift project.Thank you for answering the other question @vadian
– EmbeddedOS
Nov 19 at 15:43
please check update
– EmbeddedOS
Nov 19 at 15:30
please check update
– EmbeddedOS
Nov 19 at 15:30
The unknown context is not related to the question about keyNotFound(CodingKeys. And what is
MagicS
? To get the value for a specific key just write loginDetails["0"]!.bookieName
– vadian
Nov 19 at 15:33
The unknown context is not related to the question about keyNotFound(CodingKeys. And what is
MagicS
? To get the value for a specific key just write loginDetails["0"]!.bookieName
– vadian
Nov 19 at 15:33
MagicS is the name of my swift project.Thank you for answering the other question @vadian
– EmbeddedOS
Nov 19 at 15:43
MagicS is the name of my swift project.Thank you for answering the other question @vadian
– EmbeddedOS
Nov 19 at 15:43
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377296%2ferror-when-decoding-json-keynotfoundcodingkeys%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You aren't managing the first level with is the "11" key (for instance, or all the other ones in fact). What you can decode with your current code is something like that:
{"booked":false,"bookieName":"","bookieNumber":"","bookingTime":"10:00"}
.– Larme
Nov 19 at 15:03