0

I have data in the form of [JSON] array formate, I need to create model based on JSON and need to bind them

I try to encode the [JSON] but app is crashing with error message

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__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)
    }
}

I have created a model based on JSON data, and want to bind them with [JSON] array values

JSON is

  [
  {
    "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 based on model

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?
}
sham
  • 115
  • 9
  • 2
    update your question with `Tzlstate` class/struct definition. – Soroush Oct 29 '19 at 06:16
  • 1
    You are mixing up `SwiftyJSON` and `Codable`. Use only the latter. And **never** print `localizedDescription` in a Decoding Error catch block. Print the `error` instance. It tells you exactly what's wrong. – vadian Oct 29 '19 at 07:11
  • @Soroush i resolve the crashing issue but now I am getting an error The data couldn’t be read because it isn’t in the correct format. – sham Oct 29 '19 at 07:12
  • @vadian Yeah now I am using only Codable. and resolve the crashing with let jsonEncoder = JSONEncoder() jsonEncoder.outputFormatting = .prettyPrinted let jsonData = try? jsonEncoder.encode(m_Tzlstates). but now it has error The data couldn’t be read because it isn’t in the correct format – sham Oct 29 '19 at 07:14
  • @Soroush thanks for response now it working fine after let tzlstate = try JSONDecoder().decode([Tzlstate.self], from: data). Thanks for the response – sham Oct 29 '19 at 07:21

0 Answers0