decodable

How to parse JSON using swift 4

ε祈祈猫儿з 提交于 2020-03-11 15:10:44
问题 I am confusing to getting detail of fruit { "fruits": [ { "id": "1", "image": "https://cdn1.medicalnewstoday.com/content/images/headlines/271/271157/bananas.jpg", "name": "Banana" }, { "id": "2", "image": "http://soappotions.com/wp-content/uploads/2017/10/orange.jpg", "title": "Orange" } ] } Want to parse JSON using "Decodable" struct Fruits: Decodable { let Fruits: [fruit] } struct fruit: Decodable { let id: Int? let image: String? let name: String? } let url = URL(string: "https://www

iOS: How to use decodable for a model class with variable of type a protocol

纵饮孤独 提交于 2020-03-06 11:05:33
问题 Question: There is a model class that conforms to Decodable, this model has a variable of type someProtocol. But compiler gives an error, Compiler error Type 'MyModel' does not conform to protocol 'Decodable' Protocol requires initializer 'init(from:)' with type 'Decodable' Cannot automatically synthesize 'Decodable' because 'SomeProtocol' does not conform to 'Decodable' Code Snippet class MyModel: Decodable { var name: String? var employee: SomeProtocol? enum CodingKeys: String, CodingKey {

Parsing different JSON feeds in Swift to the same Decodable struct

江枫思渺然 提交于 2020-03-03 05:21:08
问题 I have two different JSON feeds, { "uid":9018823, "lat":43.25394, "lng":-2.93844, "bike":false, "name":"02-STATION", "address":null, "spot":true, "number":3388, "bikes":3, "booked_bikes":0, "bike_racks":20, "free_racks":16 } and { "address":null,"last_updated":1580546431, "renting":1,"returning":1,"uid":"3348"}, "free_bikes":17, "id":"0a0d9d6e93abd05548c672b60bfa9099", "latitude":40.677236, "longitude":-74.015665, "station_name":"Coffey St & Conover St", "timestamp":"2020-02-01T09:26:31

Swift Decodable, Endpoint returns completely different types

人盡茶涼 提交于 2020-02-06 08:03:56
问题 With API I'm working with, I have a case where 1 API Endpoint can return completely different responses, based on if the call was successful or not. In case of success , API Endpoint returns an Array of requested objects, in the root, something like this: [ { "key1": "value1", "key2": "value2", "key3": "value3" }, { "key1": "value1", "key2": "value2", "key3": "value3" }, ... ] which I'm normally decoding with try JSONDecoder().decode([Object].self, from: data) In case of an error , API

Swift Decodable, Endpoint returns completely different types

不想你离开。 提交于 2020-02-06 08:03:25
问题 With API I'm working with, I have a case where 1 API Endpoint can return completely different responses, based on if the call was successful or not. In case of success , API Endpoint returns an Array of requested objects, in the root, something like this: [ { "key1": "value1", "key2": "value2", "key3": "value3" }, { "key1": "value1", "key2": "value2", "key3": "value3" }, ... ] which I'm normally decoding with try JSONDecoder().decode([Object].self, from: data) In case of an error , API

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:

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 =

Parsing arbitrary format JSON date with Swift Decodable

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-17 00:27:47
问题 I am attempting to format a date in a JSON document into the format "mm-dd-yyyy". I have the following data: {"data":[{ "id": 123, "url": "https://www.google.com", "title": "The Google link", "created_at": "2017-08-29T04:00:00.000Z",//date to format "sent": true, "alternative": "https://google.com", "things": [], "description": [ "search", "lookup" ], "company": "Alphabet" }]} This is my struct: struct Sitedata: Decodable{ let data: [site] } struct site: Decodable { let id: Int let url:

Using KeyedDecodingContainer to decode an object with a random key

我是研究僧i 提交于 2020-01-06 06:45:48
问题 I have an array of objects that looks like this that I want to decode let accountPending = """ { "blocks": { "F143CCC051927EEF59EEA78D16D80F855052BBF159EA6602843904C9445": { "amount": "10000000000000000000000000000000", "source": "6xswkroybxydyzaxybb1h531sx34omiu7an9t9jy19f9mca7a36s7by5e" }, } } """.data(using: .utf8)! So I'm trying something along these lines struct PendingBlock: Decodable { let work: [String: PendingBlockData] enum CodingKeys: String, CodingKey { case work = "???" } init

How to use decodable protocol with custom type values in Swift?

本秂侑毒 提交于 2020-01-05 04:18:50
问题 I have 2 types of response depending on my reuest: First one: { "status": "success" "data": { "user_id": 2, "user_name": "John" } } And second one is: { "status": "error", "data": [], } I am using struct like that: struct ValyutaListData:Decodable { let status: String? let data: [String]? } But if response is first type response, then an error occured. Because In first Type response data is not array. It is Json object. Then i use structure like that: struct ValyutaListData:Decodable { let