Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将[JSON]转换为数据格式(Encode),以便将其与模型绑定_Ios_Swift_Decodable_Encodable - Fatal编程技术网

Ios 如何将[JSON]转换为数据格式(Encode),以便将其与模型绑定

Ios 如何将[JSON]转换为数据格式(Encode),以便将其与模型绑定,ios,swift,decodable,encodable,Ios,Swift,Decodable,Encodable,我有[JSON]数组格式的数据,我需要基于JSON创建模型并需要绑定它们 我试图对[JSON]进行编码,但应用程序因错误消息而崩溃 由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“JSON写入中的类型无效。” (uu SwiftValue)' 我已经基于JSON数据创建了一个模型,并希望将它们与[JSON]数组值绑定 JSON是 [ { "tzlLightStatus": { "tzlZones": [

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

我试图对[JSON]进行编码,但应用程序因错误消息而崩溃

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

我已经基于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?
}

Tzlstate
class/struct定义更新你的问题。你把
SwiftyJSON
Codable
搞混了。只使用后者。不要在解码错误捕获块中打印本地化描述。打印
错误
实例。它会准确地告诉您出了什么问题。@Soroush我解决了崩溃问题,但现在我遇到了一个错误,无法读取数据,因为它的格式不正确。@vadian是的,现在我只使用Codable。并使用let jsonEncoder=jsonEncoder()jsonEncoder.outputFormatting=.prettyPrinted let jsonData=try解决崩溃问题?jsonecoder.encode(m_Tzlstates)。但现在出现错误,无法读取数据,因为它的格式不正确format@Soroush感谢您现在的响应,让tzlstate=try JSONDecoder().decode([tzlstate.self],from:data)之后,它工作正常。谢谢你的回复
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?
}