codable

Is it possible to decode single level JSON into 2 separate models?

感情迁移 提交于 2020-02-04 01:06:09
问题 I have a JSON response that contains information about a user. { "userId": "123456789", "email": "\"some.email@some.domain.tld", "firstName": "\"foo\"", "lastName": "\"bar\"", "name": "\"foo bar", "bio": "\"boo baz\"", "age": "42" } I'd like to create 2 models, User and Profile from the same JSON, with a single request. I'd then like Profile to be a property on the User struct. At the moment, my standard struct looks like this - struct User: Codable, Equatable { var userId: String var email:

Swift 4 Codable Array's

自闭症网瘾萝莉.ら 提交于 2020-01-30 19:18:41
问题 So I have an API route that returns a JSON array of objects. For example: [ {"firstname": "Tom", "lastname": "Smith", "age": 31}, {"firstname": "Bob", "lastname": "Smith", "age": 28} ] I'm trying to envision how to use the new codable feature in Swift for to convert those into two objects in a class. So if I have a person class that is codable I would want to take that response and have it give me two person objects. I'm also using Alamofire to handle the requests. How can I do this? So far

Swift 4 Codable Array's

烂漫一生 提交于 2020-01-30 19:14:45
问题 So I have an API route that returns a JSON array of objects. For example: [ {"firstname": "Tom", "lastname": "Smith", "age": 31}, {"firstname": "Bob", "lastname": "Smith", "age": 28} ] I'm trying to envision how to use the new codable feature in Swift for to convert those into two objects in a class. So if I have a person class that is codable I would want to take that response and have it give me two person objects. I'm also using Alamofire to handle the requests. How can I do this? So far

Parsing json data using Codable

匆匆过客 提交于 2020-01-30 11:31:28
问题 I am new to using Codable for parsing data from JSON and I am having trouble with the format of my JSON. I am not able to parse the correct fields into my Employee object. This is my first time using codable and dealing with a complex URL. This is how my JSON url is structured: https://ibb.co/WgDNMNT { "students": [ { "uuid": "0djkdjjf734783749c", "full_name": "Joe Morris", "phone_number": "44445399", "email_address": "jm99@jfgj.com", "biography": "student of arts" }, { "uuid":

Parsing json data using Codable

牧云@^-^@ 提交于 2020-01-30 11:30:49
问题 I am new to using Codable for parsing data from JSON and I am having trouble with the format of my JSON. I am not able to parse the correct fields into my Employee object. This is my first time using codable and dealing with a complex URL. This is how my JSON url is structured: https://ibb.co/WgDNMNT { "students": [ { "uuid": "0djkdjjf734783749c", "full_name": "Joe Morris", "phone_number": "44445399", "email_address": "jm99@jfgj.com", "biography": "student of arts" }, { "uuid":

Reference Types/Subclassing, and Changes to Swift 4 Codable & encoder/decoders

烈酒焚心 提交于 2020-01-24 22:58:07
问题 I'm struggling to understand class/reference type behavior and how this relates to changes as I try to upgrade and reduce code using Codable in Swift 4. I have two classes – a SuperClass with all of the data that will be persistent and that I save to UserDefaults (a place name & string with coordinates), and a SubClass that contains additional, temporary info that I don't need (weather data for the SuperClass coordinates). In Swift 3 I used to save data like this: func saveUserDefaults() {

How to Decode selected keys manually and rest with the automatic decoding with swift Decodable?

ε祈祈猫儿з 提交于 2020-01-24 20:14:48
问题 Here is the code I am using, struct CreatePostResponseModel : Codable{ var transcodeId:String? var id:String = "" enum TopLevelCodingKeys: String, CodingKey { case _transcode = "_transcode" case _transcoder = "_transcoder" } enum CodingKeys:String, CodingKey{ case id = "_id" } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: TopLevelCodingKeys.self) if let transcodeId = try container.decodeIfPresent(String.self, forKey: ._transcode) { self.transcodeId =

Decoding unknown Encodable enum values to a default

限于喜欢 提交于 2020-01-14 10:16:07
问题 I have to deserialize a JSON string like this: { "name" : "John Smith", "value" : "someValue" } in Swift 4, where "value" should be a enum and the whole object is a struct like: struct MyType { name: String? value: Value? } At some point in the future, there might be new enum values added in the backend so I thought it would be smart to have some fallback. I thought I could create a enum like enum Value { case someValue case someOtherValue case unknown(value: String) } but I just can't wrap

How to get utf8 decoded string from Decodable?

别来无恙 提交于 2020-01-06 09:08:44
问题 The issue is that I have a json data contains and encoded string, example: let jsonData = "{ \"encoded\": \"SGVsbG8gV29ybGQh\" }".data(using: .utf8) What I need is to get the decoded value of "SGVsbG8gV29ybGQh" string. Actually I could get the desired output by implementing: let decoder = JSONDecoder() let result = try! decoder.decode(Result.self, from: jsonData!) if let data = Data(base64Encoded: result.encoded), let decodedString = String(data: data, encoding: .utf8) { print(decodedString)

How to get utf8 decoded string from Decodable?

安稳与你 提交于 2020-01-06 09:06:58
问题 The issue is that I have a json data contains and encoded string, example: let jsonData = "{ \"encoded\": \"SGVsbG8gV29ybGQh\" }".data(using: .utf8) What I need is to get the decoded value of "SGVsbG8gV29ybGQh" string. Actually I could get the desired output by implementing: let decoder = JSONDecoder() let result = try! decoder.decode(Result.self, from: jsonData!) if let data = Data(base64Encoded: result.encoded), let decodedString = String(data: data, encoding: .utf8) { print(decodedString)