I'm using Swift 5 and Xcode 11. I am attempting to parse the JSON coming back from this website, an API I am designing. http://aarontcraig-001-site1.htempurl.com/api/values
The JSON returned from that API call is this:
[
{
"Businesses": [],
"Customer": null,
"ID": 1,
"Name": "Coffee Shop",
"CustomerID": null
},
...
]
It continues an array. Some of the entries are null, others are not. However, when I parse it, they all come back nil.
Here is my code:
let url = "http://aarontcraig-001-site1.htempurl.com/api/values"
guard let finalURL = URL(string: url) else {
return
}
URLSession.shared.dataTask(with: finalURL) { (data, response, error) in
guard let data = data else {return}
do {
let myData = try JSONDecoder().decode([Value].self, from: data)
print(myData)
}
catch {
print("Error parsing \(error)")
}
and the struct I am using to catch it all:
struct Value: Codable {
let businesses : [String]?
let customer : String?
let iD : Int?
let name : String?
let customerID : String?
}
All I get back is nil values, even though clearly many of them aren't. This is what it returns.
[Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil), Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil), Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil), Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil), Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil), Sunrise.Value(businesses: nil, customer: nil, iD: nil, name: nil, customerID: nil)]
What am I doing wrong? Even if I attempt to just capture name which has a value for each entry, it sets it to nil. I am receiving the data, because if I put a breakpoint at data I can see it there.