【问题标题】:JSONDecoder keeps running error, How can I troubleshoot this? It was working last weekJSONDecoder 一直运行错误,我该如何解决这个问题?上周还在工作
【发布时间】:2020-01-22 18:50:46
【问题描述】:

我正在进行 api 调用并尝试使用我的JSONDecoder(),但它一直跳到我的位置。我该如何解决这个问题?或者其他人看到有什么问题吗?

    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")
        }

我得到的数据:

[
    {
        "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
    }]

我的结构:

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


}

【问题讨论】:

  • 而不是无意义的文字字符串"error" 打印error 实例,它告诉你出了什么问题。乍一看,我发现了两个错误。
  • 正如 vadian } catch { print(error) }所提到的
  • 知道了,谢谢!

标签: json swift xcode jsondecoder


【解决方案1】:

您的模型 HPPluCodeAdd 存在问题,我想,当您为 error 添加正确的 catch 时,您会看到它。

我建议使用 optional,因为根据 JSON,您没有一些键(例如 let commodity: String)。我建议一个基于您的 JSON 的模型(我已经检查过 - 您得到了正确的结果),但您可能需要添加更多键

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?
}

【讨论】:

  • 我不同意选项。如果密钥更改解码过程无论如何都会中断,使用非可选选项,您会收到一条全面的错误消息,以在几秒钟内解决问题。使用 optionals 什么都不会发生——你甚至可能没有注意到它——而且你也不知道为什么。
  • @vadian 嗯,你说得对,有道理,但我只是习惯于不信任后端开发人员:) 我更喜欢至少显示一些东西(当可选值时)而不是什么都不显示(如果不是可选的,但你可以看到问题的根源)。今天我们有commodity,但明天后端将从响应中删除commodity,我们无法访问我们的快速代码..用户体验不佳
猜你喜欢
  • 1970-01-01
  • 2013-11-23
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 2015-06-13
  • 2021-09-19
相关资源
最近更新 更多