jsondecoder

Swift Codable - Parse JSON array which can contain different data type

最后都变了- 提交于 2019-12-07 18:05:09
问题 I am trying to parse a JSON array which can be { "config_data": [ { "name": "illuminate", "config_title": "Blink" }, { "name": "shoot", "config_title": "Fire" } ] } or it can be of following type { "config_data": [ "illuminate", "shoot" ] } or even { "config_data": [ 25, 100 ] } So to parse this using JSONDecoder I created a struct as follows - Struct Model: Codable { var config_data: [Any]? enum CodingKeys: String, CodingKey { case config_data = "config_data" } init(from decoder: Decoder)

How to decode json with unknown key

依然范特西╮ 提交于 2019-12-06 09:18:28
everyone, how to serialize json struct, if one of field unknown? My json is: {"brands":{"any":false,"19":{"is_all":false,"values":[185,182,178],"include":true},"23":{"is_all":false,"values":[198,199,201],"include":true}},"price":[2000,10000],"year":[1990,2018],"fuel_type":[1,2],"engine_capacity":[\"1.8\",\"4.8\"],"color":[1,2,3],"gearbox_id":[2],"is_georgia":false} but: "19":{"is_all":false,"values":[185,182,178],"include":true} "23":{"is_all":false,"values":[198,199,201],"include":true} 19 and 23 - is string unknown value, it's generated by API. So my struct is: struct auto_order_model:

Is it possible to decode additional parameters using JSONDecoder?

一个人想着一个人 提交于 2019-12-06 07:40:30
We have some response returned by backend: { "name": "Some name", "number": 42, ............ "param0": value0, "param1": value1, "param2": value2 } Model structure for response: struct Model: Codable { let name: String let number: Int let params: [String: Any] } How to make JSONDecoder combine all unknown key-value pairs into params property? Decodable is incredibly powerful. It can decode completely arbitrary JSON, so this is just a sub-set of that problem. For a fully worked-out JSON Decodable , see this JSON . I'll pull the concept of Key from example, but for simplicity I'll assume that

Swift Codable - Parse JSON array which can contain different data type

岁酱吖の 提交于 2019-12-05 21:21:32
I am trying to parse a JSON array which can be { "config_data": [ { "name": "illuminate", "config_title": "Blink" }, { "name": "shoot", "config_title": "Fire" } ] } or it can be of following type { "config_data": [ "illuminate", "shoot" ] } or even { "config_data": [ 25, 100 ] } So to parse this using JSONDecoder I created a struct as follows - Struct Model: Codable { var config_data: [Any]? enum CodingKeys: String, CodingKey { case config_data = "config_data" } init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) config_data = try values.decode(

The `convertFromSnakeCase` strategy doesn't work with custom `CodingKeys` in Swift

て烟熏妆下的殇ゞ 提交于 2019-12-05 16:40:44
问题 I try to use Swift 4.1's new feature to convert snake-case to camelCase during JSON decoding. Here is the example: struct StudentInfo: Decodable { internal let studentID: String internal let name: String internal let testScore: String private enum CodingKeys: String, CodingKey { case studentID = "student_id" case name case testScore } } let jsonString = """ {"student_id":"123","name":"Apple Bay Street","test_score":"94608"} """ do { let decoder = JSONDecoder() decoder.keyDecodingStrategy =

how to handle the nil value variables

匆匆过客 提交于 2019-12-04 02:21:48
问题 I have model as below. struc Info: Decodable { var firstName: String? var lastName: String? } While displaying in tableview cell, what I am doing is as below. personName.text = "\(personArray[indexPath.row].firstName!) \(personArray[indexPath.row].lastName!)" Now the app is crashing if I have data in below format [ { "firstName" : "F 1", "lastName" : "L 1" }, { "firstName" : "F 2" }, { "lastName" : "L 3" } ] The app is crashing saying lastName is nil Solution 1 The solution for this check for

The `convertFromSnakeCase` strategy doesn't work with custom `CodingKeys` in Swift

拈花ヽ惹草 提交于 2019-12-04 01:42:10
I try to use Swift 4.1's new feature to convert snake-case to camelCase during JSON decoding. Here is the example : struct StudentInfo: Decodable { internal let studentID: String internal let name: String internal let testScore: String private enum CodingKeys: String, CodingKey { case studentID = "student_id" case name case testScore } } let jsonString = """ {"student_id":"123","name":"Apple Bay Street","test_score":"94608"} """ do { let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase let decoded = try decoder.decode(StudentInfo.self, from: Data(jsonString.utf8))

Decoding a JSON without keys in Swift 4

别来无恙 提交于 2019-12-03 17:26:22
问题 I'm using an API that returns this pretty horrible JSON: [ "A string", [ "A string", "A string", "A string", "A string", … ] ] I'm trying to decode the nested array using JSONDecoder, but it doesn't have a single key and I really don't know where to start… Do you have any idea? Thanks a lot! 回答1: If the structure stays the same, you can use this Decodable approach. First create a decodable Model like this: struct MyModel: Decodable { let firstString: String let stringArray: [String] init(from

JSON Decode does not work as expected swift 4

倖福魔咒の 提交于 2019-12-02 13:10:41
if I want to decode my JSON there is something weird happening. Here's the structs struct chatMessages : Codable { var message: [chatMessage] } struct chatMessage : Codable { var user: String var message: String var status: String var latitude: Double var longitude: Double init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) user = try values.decode(String.self, forKey: .user) message = try values.decode(String.self, forKey: .message) status = try values.decode(String.self, forKey: .status) latitude = try values.decodeIfPresent(Double.self, forKey:

Invalid JSON while parsing \n (quoted) string

孤街醉人 提交于 2019-12-02 11:42:15
问题 I have a problem with a Codable parsing... that's my example code: class Test: Codable { let resultCount: Int? let quote: String? } var json = """ { "resultCount" : 42, "quote" : "My real quote" } """.data(using: .utf8)! var decoder = JSONDecoder() let testDecoded = try! decoder.decode(Test.self, from: json) Here everything works as expected and the Test object is created. Now my back end sends me the quote string with a quote in the middle... in this form (please note \"real\" ): class Test: