jsondecoder

Why #Codable# not working in below code?

╄→гoц情女王★ 提交于 2019-12-25 18:45:59
问题 I have below code to test Codable protocol and JSONDecoder . import UIKit class ClassA: Codable { var age: Int = 1 } class ClassB: Codable { var ageInfo: ClassA? var name: String } let json4 = """ { "ageInfo": {}, "name": "Jack" } """.data(using: .utf8)! do { let d = try JSONDecoder().decode(ClassB.self, from: json4) } catch let err { print(err) } My question is, why json4 can't be decode? or how I can decode json4 ? 回答1: age in ClassA is declared non-optional so the key is required however

Is it possible to decode additional parameters using JSONDecoder?

大城市里の小女人 提交于 2019-12-22 18:22:10
问题 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? 回答1: 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

Swift : The data couldn’t be read because it isn’t in the correct format

↘锁芯ラ 提交于 2019-12-13 23:38:02
问题 I try to call the POST Api with Alamofire but it's showing me an error of incorrect format. This is my JSON response: [ { "_source": { "nome": "LOTERIAS BELEM", "endereco": "R DO COMERCIO, 279", "uf": "AL", "cidade": "BELEM", "bairro": "CENTRO" }, "_id": "010177175" }, { "_source": { "nome": "Bel Loterias" }, "_id": "80224903" }, { "_source": { "nome": "BELLEZA LOTERIAS", "endereco": "R RIVADAVIA CORREA, 498", "uf": "RS", "cidade": "SANTANA DO LIVRAMENTO", "bairro": "CENTRO" }, "_id":

Swift4 Decodable - decode half the keys as Dictionary

感情迁移 提交于 2019-12-13 17:42:05
问题 I have the situation where the server sends me a model where i know of types and names of some keys, and don't about the others. However, the user can edit those other key value pairs of their own volition. Example: { "a": "B", "b": 42, "__customKey1": "customVal1", "__customKey2": [41, 42], "__customKey3": {"z":"x"} } So what i want to end up with is a model with some declared properties and some values stuffed into a Dictionary<String, Any> , e.g. struct MyStruct { var a: String? var b: Int

Cannot invoke 'decode' with an argument list of type '(Decodable, from: Data)'

孤街浪徒 提交于 2019-12-13 04:39:47
问题 I have the following example code in a Playground. I want to decode the result of a network request if that result conforms to the Decodable protocol. Any idea why this code does not work? protocol APIRequest { associatedtype Result } func execute<T: APIRequest>(request: T) { if let decodableResult = T.Result.self as? Decodable { try JSONDecoder().decode(decodableResult, from: Data()) } } I am getting the error Cannot invoke 'decode' with an argument list of type '(Decodable, from: Data)' on

Adopting CustomNSError in DecodingError

眉间皱痕 提交于 2019-12-12 09:52:57
问题 I'm writing an error logger using Crashlytics and I've come up against an issue that is making me question my understanding of protocols and dynamic dispatch. When recording non fatal errors using Crashlytics the API expects an Error conforming object, and an optional user info dictionary. I'm looking at JSON decoding errors at the moment, and I wasn't too happy with what I was seeing in the Crashlytics dashboard when I just sent the DecodingError along in recordError. So my solution was to

Swift 4 JSONDecoder optional variable

China☆狼群 提交于 2019-12-11 14:15:34
问题 I have a Codable struct myObj : public struct VIO: Codable { let id:Int?; ... var par1:Bool = false; //default to avoid error in parsing var par2:Bool = false; } When I do receive JSON, I don't have par1 and par2 since these variables are optional. During parsing I get an error: keyNotFound(CodingKeys(stringValue: \"par1\", intValue: nil) How to solve this? 回答1: If you have local variables you have to specify the CodingKeys public struct VIO: Codable { private enum CodingKeys : String,

Decode Json Data using JsonDecoder and Alamofire

☆樱花仙子☆ 提交于 2019-12-11 08:34:37
问题 I am trying decode Json data into my Model. This is my model struct Devices : Codable { var id :String? var description :String? var status : Int? } var heroes = Devices() print(DeviceId) let loginParam: [String: Any] = [ "id": DeviceId ] let manager = Alamofire.SessionManager.default manager.session.configuration.timeoutIntervalForRequest = 5 manager.request("http://13.13.13.004/website/api/Customer/DeviceControl", method: .post , parameters: loginParam, encoding: JSONEncoding.prettyPrinted)

Unable to decode a JSON with JSON Decoder

此生再无相见时 提交于 2019-12-11 04:19:29
问题 I'm using JSON decoder to decode some Instagram users. Here's the model: struct User: Decodable { var clear_client_cache: Int var has_more: Int var hashtags: String? var places: String? var rank_token: String? var status: String? var users: [Users]? struct Users: Decodable { var position: Int var user: InstaUser? struct InstaUser: Decodable { var byline: String? var follower_count: Int? var full_name: String? var has_anonymous_profile_picture: Int? var is_verified: Int? var mutual_followers

How to decode json with unknown key

狂风中的少年 提交于 2019-12-08 03:47:09
问题 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