Failed to decode json
up vote
-1
down vote
favorite
hi I want to retrieve json data, the json data is like this
"items":[
"date_for":"2018-11-11",
"fajr":"5:28 am",
"shurooq":"6:37 am",
"dhuhr":"11:52 am",
"asr":"2:40 pm",
"maghrib":"5:06 pm",
"isha":"6:15 pm"
],
Than I make decodable for JSONDecoder like this:
struct PrayerModel: Decodable
var items: [Items]
struct Items: Decodable
var fajr: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
init(dictionary: [String: String])
self.fajr = dictionary["fajr"] ?? ""
self.dhuhr = dictionary["dhuhr"] ?? ""
self.asr = dictionary["asr"] ?? ""
self.maghrib = dictionary["maghrib"] ?? ""
self.isha = dictionary["isha"] ?? ""
and then I make an object to store decodable data:
struct Prayer
var prayerName: String
var prayerIcon: UIImage
var prayerTime: String
init(prayerName: String, prayerIcon: UIImage, prayerTime: String)
self.prayerName = prayerName
self.prayerIcon = prayerIcon
self.prayerTime = prayerTime
and retrieve it with URLSession:
do
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach( (item) in
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
)
DispatchQueue.main.async
self.tableView.reloadData()
catch let decodeErr
print("Failed to decode:", decodeErr)
and I got this error
Failed to decode: dataCorrupted(Swift.DecodingError.Context(codingPath: , debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo=NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.)))
where is the problem for my code, anyone can help?
ios json swift decodable
add a comment |
up vote
-1
down vote
favorite
hi I want to retrieve json data, the json data is like this
"items":[
"date_for":"2018-11-11",
"fajr":"5:28 am",
"shurooq":"6:37 am",
"dhuhr":"11:52 am",
"asr":"2:40 pm",
"maghrib":"5:06 pm",
"isha":"6:15 pm"
],
Than I make decodable for JSONDecoder like this:
struct PrayerModel: Decodable
var items: [Items]
struct Items: Decodable
var fajr: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
init(dictionary: [String: String])
self.fajr = dictionary["fajr"] ?? ""
self.dhuhr = dictionary["dhuhr"] ?? ""
self.asr = dictionary["asr"] ?? ""
self.maghrib = dictionary["maghrib"] ?? ""
self.isha = dictionary["isha"] ?? ""
and then I make an object to store decodable data:
struct Prayer
var prayerName: String
var prayerIcon: UIImage
var prayerTime: String
init(prayerName: String, prayerIcon: UIImage, prayerTime: String)
self.prayerName = prayerName
self.prayerIcon = prayerIcon
self.prayerTime = prayerTime
and retrieve it with URLSession:
do
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach( (item) in
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
)
DispatchQueue.main.async
self.tableView.reloadData()
catch let decodeErr
print("Failed to decode:", decodeErr)
and I got this error
Failed to decode: dataCorrupted(Swift.DecodingError.Context(codingPath: , debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo=NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.)))
where is the problem for my code, anyone can help?
ios json swift decodable
Read the error message carefully, the problem is not with your code but with the json message that is incorrect. I think it need to be surrounded by a pair of . Off topic but I don’t think you need that init method in your Items struct for decoding json
– Joakim Danielson
Nov 11 at 12:49
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
hi I want to retrieve json data, the json data is like this
"items":[
"date_for":"2018-11-11",
"fajr":"5:28 am",
"shurooq":"6:37 am",
"dhuhr":"11:52 am",
"asr":"2:40 pm",
"maghrib":"5:06 pm",
"isha":"6:15 pm"
],
Than I make decodable for JSONDecoder like this:
struct PrayerModel: Decodable
var items: [Items]
struct Items: Decodable
var fajr: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
init(dictionary: [String: String])
self.fajr = dictionary["fajr"] ?? ""
self.dhuhr = dictionary["dhuhr"] ?? ""
self.asr = dictionary["asr"] ?? ""
self.maghrib = dictionary["maghrib"] ?? ""
self.isha = dictionary["isha"] ?? ""
and then I make an object to store decodable data:
struct Prayer
var prayerName: String
var prayerIcon: UIImage
var prayerTime: String
init(prayerName: String, prayerIcon: UIImage, prayerTime: String)
self.prayerName = prayerName
self.prayerIcon = prayerIcon
self.prayerTime = prayerTime
and retrieve it with URLSession:
do
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach( (item) in
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
)
DispatchQueue.main.async
self.tableView.reloadData()
catch let decodeErr
print("Failed to decode:", decodeErr)
and I got this error
Failed to decode: dataCorrupted(Swift.DecodingError.Context(codingPath: , debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo=NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.)))
where is the problem for my code, anyone can help?
ios json swift decodable
hi I want to retrieve json data, the json data is like this
"items":[
"date_for":"2018-11-11",
"fajr":"5:28 am",
"shurooq":"6:37 am",
"dhuhr":"11:52 am",
"asr":"2:40 pm",
"maghrib":"5:06 pm",
"isha":"6:15 pm"
],
Than I make decodable for JSONDecoder like this:
struct PrayerModel: Decodable
var items: [Items]
struct Items: Decodable
var fajr: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
init(dictionary: [String: String])
self.fajr = dictionary["fajr"] ?? ""
self.dhuhr = dictionary["dhuhr"] ?? ""
self.asr = dictionary["asr"] ?? ""
self.maghrib = dictionary["maghrib"] ?? ""
self.isha = dictionary["isha"] ?? ""
and then I make an object to store decodable data:
struct Prayer
var prayerName: String
var prayerIcon: UIImage
var prayerTime: String
init(prayerName: String, prayerIcon: UIImage, prayerTime: String)
self.prayerName = prayerName
self.prayerIcon = prayerIcon
self.prayerTime = prayerTime
and retrieve it with URLSession:
do
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
prayerData.items.forEach( (item) in
let isya = Prayer(prayerName: "Isya", prayerIcon: #imageLiteral(resourceName: "Clear"), prayerTime: item.isha)
self.prayers.append(contentsOf: [shubuh, dzuhur, ashar, maghrib, isya])
)
DispatchQueue.main.async
self.tableView.reloadData()
catch let decodeErr
print("Failed to decode:", decodeErr)
and I got this error
Failed to decode: dataCorrupted(Swift.DecodingError.Context(codingPath: , debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo=NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.)))
where is the problem for my code, anyone can help?
ios json swift decodable
ios json swift decodable
edited Nov 11 at 15:04
Daniel Larsson
549419
549419
asked Nov 11 at 12:35
ferryawijayanto
587
587
Read the error message carefully, the problem is not with your code but with the json message that is incorrect. I think it need to be surrounded by a pair of . Off topic but I don’t think you need that init method in your Items struct for decoding json
– Joakim Danielson
Nov 11 at 12:49
add a comment |
Read the error message carefully, the problem is not with your code but with the json message that is incorrect. I think it need to be surrounded by a pair of . Off topic but I don’t think you need that init method in your Items struct for decoding json
– Joakim Danielson
Nov 11 at 12:49
Read the error message carefully, the problem is not with your code but with the json message that is incorrect. I think it need to be surrounded by a pair of . Off topic but I don’t think you need that init method in your Items struct for decoding json
– Joakim Danielson
Nov 11 at 12:49
Read the error message carefully, the problem is not with your code but with the json message that is incorrect. I think it need to be surrounded by a pair of . Off topic but I don’t think you need that init method in your Items struct for decoding json
– Joakim Danielson
Nov 11 at 12:49
add a comment |
2 Answers
2
active
oldest
votes
up vote
-1
down vote
accepted
If you want to use Decodable prototype with your JSON, than variables should have exact the same name and include all items with same position.
Try this:
var date_for: String
var fajr: String
var shurooq: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
init(dictionary: [String: String])
self.date_for = dictionary["date_for"] ?? ""
self.fajr = dictionary["fajr"] ?? ""
self.shurooq = dictionary["shurooq"] ?? ""
self.dhuhr = dictionary["dhuhr"] ?? ""
self.asr = dictionary["asr"] ?? ""
self.maghrib = dictionary["maghrib"] ?? ""
self.isha = dictionary["isha"] ?? ""
thank you, I thought since I need only a bit part of data I can skip it. thank you it works!
– ferryawijayanto
Nov 11 at 13:16
This answer is not relevant to the question since you do not have to include all the same items so it wouldn't generate an error and definitely not the error in the question.
– Joakim Danielson
Nov 11 at 15:43
@ferryawijayanto, you can skip fields you don’t need.
– Joakim Danielson
Nov 12 at 19:24
add a comment |
up vote
0
down vote
Like I mentioned in my comment the json message is incorrect, I added to it and it worked fine.
"items":[
"date_for":"2018-11-11",
"fajr":"5:28 am",
"shurooq":"6:37 am",
"dhuhr":"11:52 am",
"asr":"2:40 pm",
"maghrib":"5:06 pm",
"isha":"6:15 pm"
]
And you do not have to include all elements in the json message if you do not want to use them, I removed the init() method from Items since it is not used.
struct Items: Decodable
var fajr: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
Then it worked perfectly fine with the below code
let str = ""items":["date_for":"2018-11-11","fajr":"5:28 am","shurooq":"6:37 am","dhuhr":"11:52 am","asr":"2:40 pm","maghrib":"5:06 pm","isha":"6:15 pm"]"
do
if let data = str.data(using: .utf8)
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
for item in prayerData.items
print(item)
catch let decodeErr
print("Failed to decode:", decodeErr)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
accepted
If you want to use Decodable prototype with your JSON, than variables should have exact the same name and include all items with same position.
Try this:
var date_for: String
var fajr: String
var shurooq: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
init(dictionary: [String: String])
self.date_for = dictionary["date_for"] ?? ""
self.fajr = dictionary["fajr"] ?? ""
self.shurooq = dictionary["shurooq"] ?? ""
self.dhuhr = dictionary["dhuhr"] ?? ""
self.asr = dictionary["asr"] ?? ""
self.maghrib = dictionary["maghrib"] ?? ""
self.isha = dictionary["isha"] ?? ""
thank you, I thought since I need only a bit part of data I can skip it. thank you it works!
– ferryawijayanto
Nov 11 at 13:16
This answer is not relevant to the question since you do not have to include all the same items so it wouldn't generate an error and definitely not the error in the question.
– Joakim Danielson
Nov 11 at 15:43
@ferryawijayanto, you can skip fields you don’t need.
– Joakim Danielson
Nov 12 at 19:24
add a comment |
up vote
-1
down vote
accepted
If you want to use Decodable prototype with your JSON, than variables should have exact the same name and include all items with same position.
Try this:
var date_for: String
var fajr: String
var shurooq: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
init(dictionary: [String: String])
self.date_for = dictionary["date_for"] ?? ""
self.fajr = dictionary["fajr"] ?? ""
self.shurooq = dictionary["shurooq"] ?? ""
self.dhuhr = dictionary["dhuhr"] ?? ""
self.asr = dictionary["asr"] ?? ""
self.maghrib = dictionary["maghrib"] ?? ""
self.isha = dictionary["isha"] ?? ""
thank you, I thought since I need only a bit part of data I can skip it. thank you it works!
– ferryawijayanto
Nov 11 at 13:16
This answer is not relevant to the question since you do not have to include all the same items so it wouldn't generate an error and definitely not the error in the question.
– Joakim Danielson
Nov 11 at 15:43
@ferryawijayanto, you can skip fields you don’t need.
– Joakim Danielson
Nov 12 at 19:24
add a comment |
up vote
-1
down vote
accepted
up vote
-1
down vote
accepted
If you want to use Decodable prototype with your JSON, than variables should have exact the same name and include all items with same position.
Try this:
var date_for: String
var fajr: String
var shurooq: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
init(dictionary: [String: String])
self.date_for = dictionary["date_for"] ?? ""
self.fajr = dictionary["fajr"] ?? ""
self.shurooq = dictionary["shurooq"] ?? ""
self.dhuhr = dictionary["dhuhr"] ?? ""
self.asr = dictionary["asr"] ?? ""
self.maghrib = dictionary["maghrib"] ?? ""
self.isha = dictionary["isha"] ?? ""
If you want to use Decodable prototype with your JSON, than variables should have exact the same name and include all items with same position.
Try this:
var date_for: String
var fajr: String
var shurooq: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
init(dictionary: [String: String])
self.date_for = dictionary["date_for"] ?? ""
self.fajr = dictionary["fajr"] ?? ""
self.shurooq = dictionary["shurooq"] ?? ""
self.dhuhr = dictionary["dhuhr"] ?? ""
self.asr = dictionary["asr"] ?? ""
self.maghrib = dictionary["maghrib"] ?? ""
self.isha = dictionary["isha"] ?? ""
answered Nov 11 at 13:00
Den Andreychuk
587
587
thank you, I thought since I need only a bit part of data I can skip it. thank you it works!
– ferryawijayanto
Nov 11 at 13:16
This answer is not relevant to the question since you do not have to include all the same items so it wouldn't generate an error and definitely not the error in the question.
– Joakim Danielson
Nov 11 at 15:43
@ferryawijayanto, you can skip fields you don’t need.
– Joakim Danielson
Nov 12 at 19:24
add a comment |
thank you, I thought since I need only a bit part of data I can skip it. thank you it works!
– ferryawijayanto
Nov 11 at 13:16
This answer is not relevant to the question since you do not have to include all the same items so it wouldn't generate an error and definitely not the error in the question.
– Joakim Danielson
Nov 11 at 15:43
@ferryawijayanto, you can skip fields you don’t need.
– Joakim Danielson
Nov 12 at 19:24
thank you, I thought since I need only a bit part of data I can skip it. thank you it works!
– ferryawijayanto
Nov 11 at 13:16
thank you, I thought since I need only a bit part of data I can skip it. thank you it works!
– ferryawijayanto
Nov 11 at 13:16
This answer is not relevant to the question since you do not have to include all the same items so it wouldn't generate an error and definitely not the error in the question.
– Joakim Danielson
Nov 11 at 15:43
This answer is not relevant to the question since you do not have to include all the same items so it wouldn't generate an error and definitely not the error in the question.
– Joakim Danielson
Nov 11 at 15:43
@ferryawijayanto, you can skip fields you don’t need.
– Joakim Danielson
Nov 12 at 19:24
@ferryawijayanto, you can skip fields you don’t need.
– Joakim Danielson
Nov 12 at 19:24
add a comment |
up vote
0
down vote
Like I mentioned in my comment the json message is incorrect, I added to it and it worked fine.
"items":[
"date_for":"2018-11-11",
"fajr":"5:28 am",
"shurooq":"6:37 am",
"dhuhr":"11:52 am",
"asr":"2:40 pm",
"maghrib":"5:06 pm",
"isha":"6:15 pm"
]
And you do not have to include all elements in the json message if you do not want to use them, I removed the init() method from Items since it is not used.
struct Items: Decodable
var fajr: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
Then it worked perfectly fine with the below code
let str = ""items":["date_for":"2018-11-11","fajr":"5:28 am","shurooq":"6:37 am","dhuhr":"11:52 am","asr":"2:40 pm","maghrib":"5:06 pm","isha":"6:15 pm"]"
do
if let data = str.data(using: .utf8)
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
for item in prayerData.items
print(item)
catch let decodeErr
print("Failed to decode:", decodeErr)
add a comment |
up vote
0
down vote
Like I mentioned in my comment the json message is incorrect, I added to it and it worked fine.
"items":[
"date_for":"2018-11-11",
"fajr":"5:28 am",
"shurooq":"6:37 am",
"dhuhr":"11:52 am",
"asr":"2:40 pm",
"maghrib":"5:06 pm",
"isha":"6:15 pm"
]
And you do not have to include all elements in the json message if you do not want to use them, I removed the init() method from Items since it is not used.
struct Items: Decodable
var fajr: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
Then it worked perfectly fine with the below code
let str = ""items":["date_for":"2018-11-11","fajr":"5:28 am","shurooq":"6:37 am","dhuhr":"11:52 am","asr":"2:40 pm","maghrib":"5:06 pm","isha":"6:15 pm"]"
do
if let data = str.data(using: .utf8)
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
for item in prayerData.items
print(item)
catch let decodeErr
print("Failed to decode:", decodeErr)
add a comment |
up vote
0
down vote
up vote
0
down vote
Like I mentioned in my comment the json message is incorrect, I added to it and it worked fine.
"items":[
"date_for":"2018-11-11",
"fajr":"5:28 am",
"shurooq":"6:37 am",
"dhuhr":"11:52 am",
"asr":"2:40 pm",
"maghrib":"5:06 pm",
"isha":"6:15 pm"
]
And you do not have to include all elements in the json message if you do not want to use them, I removed the init() method from Items since it is not used.
struct Items: Decodable
var fajr: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
Then it worked perfectly fine with the below code
let str = ""items":["date_for":"2018-11-11","fajr":"5:28 am","shurooq":"6:37 am","dhuhr":"11:52 am","asr":"2:40 pm","maghrib":"5:06 pm","isha":"6:15 pm"]"
do
if let data = str.data(using: .utf8)
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
for item in prayerData.items
print(item)
catch let decodeErr
print("Failed to decode:", decodeErr)
Like I mentioned in my comment the json message is incorrect, I added to it and it worked fine.
"items":[
"date_for":"2018-11-11",
"fajr":"5:28 am",
"shurooq":"6:37 am",
"dhuhr":"11:52 am",
"asr":"2:40 pm",
"maghrib":"5:06 pm",
"isha":"6:15 pm"
]
And you do not have to include all elements in the json message if you do not want to use them, I removed the init() method from Items since it is not used.
struct Items: Decodable
var fajr: String
var dhuhr: String
var asr: String
var maghrib: String
var isha: String
Then it worked perfectly fine with the below code
let str = ""items":["date_for":"2018-11-11","fajr":"5:28 am","shurooq":"6:37 am","dhuhr":"11:52 am","asr":"2:40 pm","maghrib":"5:06 pm","isha":"6:15 pm"]"
do
if let data = str.data(using: .utf8)
let prayerData = try JSONDecoder().decode(PrayerModel.self, from: data)
for item in prayerData.items
print(item)
catch let decodeErr
print("Failed to decode:", decodeErr)
answered Nov 11 at 15:44
Joakim Danielson
5,8303621
5,8303621
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53248815%2ffailed-to-decode-json%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
Read the error message carefully, the problem is not with your code but with the json message that is incorrect. I think it need to be surrounded by a pair of . Off topic but I don’t think you need that init method in your Items struct for decoding json
– Joakim Danielson
Nov 11 at 12:49