0

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.

3
  • There's no array of Component in your JSON data, so your model struct doesn't match the data. Don't use try? when parsing, catch and print any errors to see what's going wrong. Commented Jul 23, 2019 at 19:55
  • Look at the JSON, there is not key 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. Commented Jul 23, 2019 at 19:58
  • how do I change the struct to match the data? Commented Jul 23, 2019 at 20:07

1 Answer 1

1

You need

var unitComponents  = [Component]()

do { 
    let jsonUnits = try JSONDecoder().decode([String:Component].self, from: data) 
    unitComponents = Array(jsonUnits.values)
    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
}
catch {
  print(error)
}

struct Component: Codable {
   let sku, qty, desc, condition: String

   enum CodingKeys: String, CodingKey {
       case sku = "Sku"
       case qty = "Qty"
       case desc = "Desc"
       case condition = "Condition"
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm still having issues with this displaying the data in a table view. It appears like all the unitComponents do contain the parsed data, but perhaps because of the enum struct I am not getting it to write to the table cell correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.