Hello,
I have a json array showing in Xcode debugger (from the line "print(dataString)"):
Optional("[{\"id\":\"8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w\",\"name\":\"Ameliana\",\"country\":\"France\",\"type\":\"Private\\/Corporate\",\"overallrecsit\":\"Positive\",\"dlastupd\":\"1741351633\",\"doverallrecsit\":\"1546848000\"},{\"id\":\"z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68\",\"name\":\"Timberland\",\"country\":\"Switzerland\",\"type\":\"Charter\",\"overallrecsit\":\"Negative\",\"dlastupd\":\"1741351615\",\"doverallrecsit\":\"1740434582\"},
But my JSON decoder is throwing the catch error "Error in JSON parsing"
This is the code:
super.viewDidLoad()
let urlString = "https://www.pilotjobsnetwork.com/service_ios.php"
let url = URL(string: urlString)
guard url != nil else {
return
}
let session = URLSession.shared
let dataTask = session.dataTask(with: url!) { (data, response, error) in
var dataString = String(data: data!, encoding: String.Encoding.utf8)
print(dataString)
if error == nil && data != nil {
// Parse JSON
let decoder = JSONDecoder()
do {
let newsFeed = try decoder.decode(NewsFeed.self, from: data!)
print(newsFeed)
print(error)
}
catch{
print("Error in JSON parsing")
}
}
}
// Make the API Call
dataTask.resume()
}
And this is my Codable file NewsFeed.swift:
struct NewsFeed: Codable {
var id: String
var name: String
var country: String
var type: String
var overallrecsit: String
var dlastupd: String
var doverallrecsit: String
}
Please do you know why the parsing may be failing? Is it significant that in the debugging window the JSON is displaying backslashes before the quotation marks?
Thank you for any pointers :-)
In situations like this I:
-
Check whether the JSON itself is valid.
-
Then put it into a small test program and debug from there.
The JSON you posted was truncated, but I grabbed what I could and run it through jq
:
% cat test.json
[{"id":"8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w","name":"Ameliana","country":"France","type":"Private\\/Corporate","overallrecsit":"Positive","dlastupd":"1741351633","doverallrecsit":"1546848000"},{"id":"z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68","name":"Timberland","country":"Switzerland","type":"Charter","overallrecsit":"Negative","dlastupd":"1741351615","doverallrecsit":"1740434582"}]
% jq . test.json
[
{
"id": "8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w",
"name": "Ameliana",
"country": "France",
"type": "Private\\/Corporate",
"overallrecsit": "Positive",
"dlastupd": "1741351633",
"doverallrecsit": "1546848000"
},
{
"id": "z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68",
"name": "Timberland",
"country": "Switzerland",
"type": "Charter",
"overallrecsit": "Negative",
"dlastupd": "1741351615",
"doverallrecsit": "1740434582"
}
]
So far so good. I then created this program:
import Foundation
let json = """
[{"id":"8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w","name":"Ameliana","country":"France","type":"Private\\/Corporate","overallrecsit":"Positive","dlastupd":"1741351633","doverallrecsit":"1546848000"},{"id":"z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68","name":"Timberland","country":"Switzerland","type":"Charter","overallrecsit":"Negative","dlastupd":"1741351615","doverallrecsit":"1740434582"}]
"""
struct NewsFeed: Codable {
var id: String
var name: String
var country: String
var type: String
var overallrecsit: String
var dlastupd: String
var doverallrecsit: String
}
func main() throws {
let feeds = try JSONDecoder().decode([NewsFeed].self, from: Data(json.utf8))
print(feeds)
}
try main()
It works as well, printing:
[xxst.NewsFeed(id: "8e8tfdcssu4u2hn7a71tkveahjhn8xghqcfkwf1bzvtrw5nu0b89w", name: "Ameliana", country: "France", type: "Private/Corporate", overallrecsit: "Positive", dlastupd: "1741351633", doverallrecsit: "1546848000"), xxst.NewsFeed(id: "z69718a1a5z2y5czkwrhr1u37h7h768v05qr3pf1fegh4r4yrt5a68", name: "Timberland", country: "Switzerland", type: "Charter", overallrecsit: "Negative", dlastupd: "1741351615", doverallrecsit: "1740434582")]
I’m not sure why this is failing in your real app, but it’s probably something to do with the JSON data that you elided. My advice is that follow the above process and see where that leads you.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"