0

我有 [JSON] 数组格式的数据,我需要基于 JSON 创建模型并需要绑定它们

我尝试对 [JSON] 进行编码,但应用程序崩溃并显示错误消息

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“JSON 写入中的类型无效 (__SwiftValue)”

 func getDataForTzlState(m_Tzlstates: [JSON]) {
    do {
       let jsonEncoder = JSONEncoder()
        jsonEncoder.outputFormatting = .prettyPrinted
        let jsonData = try? jsonEncoder.encode(m_Tzlstates)
        guard let data = jsonData else {return}
        let tzlstate = try JSONDecoder().decode(Tzlstate.self, from: data)
       print(tzlstate)
    } catch {
        print(error.localizedDescription)
    }
}

我创建了一个基于 JSON 数据的模型,并希望将它们与 [JSON] 数组值绑定

JSON是

  [
  {
    "tzlLightStatus": {
      "tzlZones": [
        {
          "state": "OFF",
          "red": 0,
          "green": 0,
          "zoneId": "1",
          "intensity": 0,
          "speed": 0,
          "blue": 0
        },
        {
          "state": "OFF",
          "red": 0,
          "green": 0,
          "zoneId": "2",
          "intensity": 0,
          "speed": 0,
          "blue": 0
        },
        {
          "state": "OFF",
          "red": 0,
          "green": 0,
          "zoneId": "3",
          "intensity": 0,
          "speed": 0,
          "blue": 0
        }
      ]`enter code here`
    },
    "updateTimestamp": "2019-10-29T05:17:03.517+0000",
    "tzlColorSettings": {
      "tzlColors": [
        {
          "red": 255,
          "blue": 255,
          "colorId": 1,
          "secondary": false,
          "green": 255
        },
        {
          "red": 255,
          "blue": 0,
          "colorId": 2,
          "secondary": false,
          "green": 0
        },
        {
          "red": 255,
          "blue": 255,
          "colorId": 3,
          "secondary": false,
          "green": 0
        },
        {
          "red": 0,
          "blue": 255,
          "colorId": 4,
          "secondary": false,
          "green": 0
        },
        {
          "red": 0,
          "blue": 255,
          "colorId": 5,
          "secondary": false,
          "green": 255
        },
        {
          "red": 0,
          "blue": 0,
          "colorId": 6,
          "secondary": false,
          "green": 255
        },
        {
          "red": 255,
          "blue": 0,
          "colorId": 7,
          "secondary": false,
          "green": 100
        },
        {
          "red": 255,
          "blue": 0,
          "colorId": 8,
          "secondary": false,
          "green": 255
        }
      ]
    },
    "tzlConfiguration": {
      "tzlGroupStates": [
        {
          "state": "AB",
          "groupId": "1"
        },
        {
          "state": "BC",
          "groupId": "2"
        },
        {
          "state": "AC",
          "groupId": "3"
        },
        {
          "state": "ABC",
          "groupId": "4"
        }
      ],
      "tzlZoneFunctions": [
        {
          "zoneId": "1",
          "zoneFunction": "NORMAL"
        },
        {
          "zoneId": "2",
          "zoneFunction": "NORMAL"
        },
        {
          "zoneId": "3",
          "zoneFunction": "NORMAL"
        }
      ]
    }
  }
]

基于模型的结构

struct Tzlstate: Codable {
    var tzlLightStatus: TzlLightStatus?
    var updateTimestamp: String?
    var tzlColorSettings: TzlColorSetting?
    var tzlConfiguration: TzlConfiguration?
}
struct TzlColorSetting: Codable {
    var tzlColors: [TzlColor]?
}

struct TzlConfiguration: Codable {
    var tzlGroupStates: [TzlGroupState]?
    var tzlZoneFunctions: [TzlZoneFunction]?
}
struct TzlColor: Codable {
    var red: Int?
    var blue: Int?
    var colorId: Int?
    var secondary: Bool?
    var green: Int?
}
struct TzlLightStatus: Codable {
    var tzlZones: [TzlZone]?
}
struct TzlZone: Codable {
    var state: String?
    var red: Int?
    var green: Int?
    var zoneId: String?
    var intensity: Int?
    var speed: Int?
    var blue: Int?
}
struct TzlZoneFunction: Codable {
    var zoneId: String?
    var zoneFunction: String?
}

struct TzlGroupState: Codable {
    var state: String?
    var groupId: String?
}
4

0 回答 0