My app has a few different view controllers that receive JSON data from my web service and parses it into table views. This one isn't working.
Here is a sample of the JSON data I am trying to parse into a TableViewController
"content_4_4":{"Sku":"W-22","Qty":"1","Desc":"Panel","Condition":""},"content_4_5":{"Sku":"W-15","Qty":"1","Desc":"Desk 44\" long","Condition":""},"content_4_6":{"Sku":"W-18","Qty":"1","Desc":"End Return Panel","Condition":""},"content_4_7":{"Sku":"W-25","Qty":"1","Desc":"End Return Panel","Condition":""},"content_4_8":{"Sku":"W-19","Qty":"1","Desc":"Header w/lights, transformer","Condition":""}
Here is the codable struct I am using to model the data.
struct Components: Codable {
var result: [Component]
}
struct Component: Codable {
var Sku: String
var Qty: String
var Desc: String
var Condition: String
}
Here is how I am trying to parse the json into components
let decoder = JSONDecoder()
if let jsonUnits = try? decoder.decode(Components.self, from: data) {
UnitComponents = jsonUnits.result
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
Nothing is showing up in my reusable cell. Since I can see the data I'm sure I'm parsing it wrong or something in the data could be breaking the parser. Perhaps the forward slashes or the presence of the titles ex. "content_4_4" is breaking the parser. Unsure. Any help appreciated.
Componentin your JSON data, so your model struct doesn't match the data. Don't usetry?when parsing, catch and print any errors to see what's going wrong.result, decode[String:Component].self. And I totally agree with Gereon: Don't use try? when parsing, never do that. And use CodingKeys to map the keys to lowercased variables.