小编Mic*_*gal的帖子

使用 Codable 问题解码 JSON。keyNotFound 错误信息

我在解码 JSON 时遇到问题。我正在尝试解码我​​的 JSON

let temp = try JSONDecoder().decode([LastTemperatureResponse].self, from: data).

我的Codable结构如下:

struct LastTemperatureResponseElement: Codable {
    let measurement: Measurement
}

struct Measurement: Codable {
    let ts: String
    let sensors: [VportSensor]
}

struct VportSensor: TemperatureSensor, Codable {
    var lastUpdate: String!

    let address, description: String
    let status: String
    let temperature: Double
} 
Run Code Online (Sandbox Code Playgroud)

好吧,如果我试图解码我的 JSON,我会收到很清楚的错误消息

keyNotFound(CodingKeys(stringValue: "status", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "measurement", intValue: nil), CodingKeys(stringValue: "sensors", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No …
Run Code Online (Sandbox Code Playgroud)

json swift codable

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

codable ×1

json ×1

swift ×1