-1

I am making an api call and trying to use my JSONDecoder() and it it keeps jumping to my catch. How can I troubleshoot this? Or does anyone else see whats wrong?

    do{

        print("decoding")
        let decoder = JSONDecoder()
        decoder.keyDecodingStrategy = .convertFromSnakeCase
        let commoditiesList = try decoder.decode([HPPluCodeAdd].self, from: data!)
        print(commoditiesList)

        self.harvestCommodities = commoditiesList
        DispatchQueue.main.async {
            self.AddHarvestPlanPluCodeTable.reloadData()
            print("fin")
        }
    }catch _{
        print("error")
        }

The data I am getting :

[
    {
        "cases_per_week_avg": 0,
        "target": 1200,
        "pounds_per_case": 35,
        "repeat_harvest": true,
        "cases_per_week": "0",
        "lbs_per_week": "0",
        "id": 14,
        "acres": 800,
        "plu_code": 12188,
        "active": true,
        "options": 1,
        "plant_days": 80,
        "plu_code_commodity": "Organic Cabbage",
        "plu_code_variety": "Green",
        "cases_per_pallette": 42,
        "harvest_week_count": 1,
        "pounds_per_acre": 32767
    },
    {
        "cases_per_week_avg": 0,
        "target": 0,
        "pounds_per_case": 20,
        "repeat_harvest": true,
        "cases_per_week": "0",
        "lbs_per_week": "0",
        "id": 20,
        "acres": 800,
        "plu_code": 12187,
        "active": true,
        "options": 1,
        "plant_days": 80,
        "plu_code_commodity": "Organic Broccoli",
        "plu_code_variety": "",
        "cases_per_pallette": 48,
        "harvest_week_count": 1,
        "pounds_per_acre": 16000
    }]

My Struct:

struct HPPluCodeAdd : Decodable{
    var id : Int
    var commodity: String
    var casesPerWeekAvg: Int
    var repeatHarvest: Int
    var casesPerWeek: Double
    var lbsPerWeek :Double
    var acres : Int
    var pluCode : Int
    var active :Bool
    var options: Int
    var plantDays: Int
    var pluCodeCommodity: String
    var pluCodeVariety: String
    var casesPerPalette: Int
    var harvestWeekCount : Int
    var poundsPerAcre: Int
    var poundsPerCase : Int


}
B25Dec
  • 2,301
  • 5
  • 31
  • 54
Andy Nguyen
  • 451
  • 5
  • 17

1 Answers1

0

You have the issue with your model HPPluCodeAdd and, I guess, you'll see it when you'll add correct catch for error.

I'd recommend use optional, because according to JSON you haven't some keys (like let commodity: String). I suggest a model based on your JSON (I've checked - you get a correct result), but you probably need to add some more keys

struct HPPluCodeAdd: Decodable {
    let casesPerWeekAvg, target, poundsPerCase: Int?
    let repeatHarvest: Bool?
    let commodity: String?
    let casesPerWeek, lbsPerWeek: String?
    let id, acres, pluCode: Int?
    let active: Bool?
    let options, plantDays: Int?
    let pluCodeCommodity, pluCodeVariety: String?
    let casesPerPallette, harvestWeekCount, poundsPerAcre: Int?
}
Vadim Nikolaev
  • 2,132
  • 17
  • 34
  • I disagree regarding optionals. If keys change the decoding process breaks anyway, With non-optionals you get a comprehensive error message to fix the issue in seconds. With optionals nothing happens – you might not even notice it – and you have no clue why. – vadian Jan 23 '20 at 09:11
  • @vadian hmm, you're right, make sense, but I’m just used to not trusting the backend developers:) and I prefer to show at least something (when the optional value) than nothing (if not optional, but you can see the source of issue). Today we have `commodity`, but tomorrow backend will remove `commodity` from the response and we don’t have access to our swift code..user gets bad UX – Vadim Nikolaev Jan 23 '20 at 09:26