codable

Swift 4 decoding json using Codable

北战南征 提交于 2019-11-27 04:54:14
Can someone tell me what I'm doing wrong? I've looked at all the questions on here like from here How to decode a nested JSON struct with Swift Decodable protocol? and I've found one that seems exactly what I need Swift 4 Codable decoding json . { "success": true, "message": "got the locations!", "data": { "LocationList": [ { "LocID": 1, "LocName": "Downtown" }, { "LocID": 2, "LocName": "Uptown" }, { "LocID": 3, "LocName": "Midtown" } ] } } struct Location: Codable { var data: [LocationList] } struct LocationList: Codable { var LocID: Int! var LocName: String! } class ViewController:

Swift Codable with dynamic keys

血红的双手。 提交于 2019-11-27 03:38:04
问题 I have JSON structure as: "periods": { "2018-06-07": [ { "firstName": "Test1", "lastName": "Test1" } ], "2018-06-06": [ { "firstName": "Test1", "lastName": "Test1" } ] } I tried to parse it like this: public struct Schedule: Codable { public let periods: Periods } public struct Periods: Codable { public let unknown: [Inner] public struct Inner: Codable { public let firstName: String public let lastName: String } private struct CustomCodingKeys: CodingKey { var stringValue: String init?

Swift Codable multiple types

僤鯓⒐⒋嵵緔 提交于 2019-11-27 03:30:39
问题 I try to parse an api returning a json object. My problem is that some keys are sometime a string, sometime an object like the key "Value" in the following example: [ { "Description": null, "Group": "Beskrivning av enheten", "GroupDescription": null, "Id": "Description", "Name": "Mer om enheten", "Value": "Det finns möjlighet till parkering på gatorna runt om, men det är kantstenar och ganska branta backar för att komma upp till lekplatsen.\r\n\r\nUtanför själva lekplatsen finns en

Swift's JSONDecoder with multiple date formats in a JSON string?

穿精又带淫゛_ 提交于 2019-11-27 02:07:46
问题 Swift's JSONDecoder offers a dateDecodingStrategy property, which allows us to define how to interpret incoming date strings in accordance with a DateFormatter object. However, I am currently working with an API that returns both date strings ( yyyy-MM-dd ) and datetime strings ( yyyy-MM-dd HH:mm:ss ), depending on the property. Is there a way to have the JSONDecoder handle this, since the provided DateFormatter object can only deal with a single dateFormat at a time? One ham-handed solution

Encode/Decode Array of Types conforming to protocol with JSONEncoder

老子叫甜甜 提交于 2019-11-27 00:06:18
I'm trying to find the best way to Encode/Decode an array of structs conforming to a swift protocol using the new JSONDecoder/Encoder in Swift 4. I made up a little example to illustrate the problem: First we have a protocol Tag and some Types that conform to this protocol. protocol Tag: Codable { var type: String { get } var value: String { get } } struct AuthorTag: Tag { let type = "author" let value: String } struct GenreTag: Tag { let type = "genre" let value: String } Then we have a Type Article which has an Array of Tags. struct Article: Codable { let tags: [Tag] let title: String }

Expected to decode Array<Any> but found a dictionary instead

自闭症网瘾萝莉.ら 提交于 2019-11-26 21:02:37
问题 I was fetching data from an API returning an array but needed to replace it by an API that has "sub levels": RAW: ETH: USD: TYPE: "5" MARKET: "CCCAGG" FROMSYMBOL: "ETH" TOSYMBOL: "USD" PRICE: 680.89 CHANGEPCT24HOUR : -9.313816893529749 Here is my struct: struct Ethereum: Codable { let percentChange24h: String let priceUSD: String private enum CodingKeys: String, CodingKey { case priceUSD = "PRICE", percentChange24h = "CHANGEPCT24HOUR" } } And the implementation: func fetchEthereumInfo

Using Codable on a dynamic type/object

 ̄綄美尐妖づ 提交于 2019-11-26 20:44:53
问题 Hi I have the following structure nested in a bigger structure that is returned from an api call but I can't manage to encode/decode this part. The problem I am having is that the customKey and customValue are both dynamic. { "current" : "a value" "hash" : "some value" "values": { "customkey": "customValue", "customKey": "customValue" } } I tried something like var values: [String:String] But that is obviously not working because its not actually an array of [String:String] . 回答1: Since you

Swift 4 Codable; How to decode object with single root-level key

二次信任 提交于 2019-11-26 15:45:55
问题 I'm using the Swift 4 Codable protocol with JSON data. My data is formatted such that there is a single key at the root level with an object value containing the properties I need, such as: { "user": { "id": 1, "username": "jdoe" } } I have a User struct that can decode the user key: struct User: Codable { let id: Int let username: String } Since id and username are properties of user , not at the root level, I needed to make a wrapper type like so: struct UserWrapper: Codable { let user:

How to decode a property with type of JSON dictionary in Swift 4 decodable protocol

余生颓废 提交于 2019-11-26 15:04:20
Let's say I have Customer data type which contains a metadata property that can contains any JSON dictionary in the customer object struct Customer { let id: String let email: String let metadata: [String: Any] } { "object": "customer", "id": "4yq6txdpfadhbaqnwp3", "email": "john.doe@example.com", "metadata": { "link_id": "linked-id", "buy_count": 4 } } The metadata property can be any arbitrary JSON map object. Before I can cast the property from a deserialized JSON from NSJSONDeserialization but with the new Swift 4 Decodable protocol, I still can't think of a way to do that. Do anyone know

How to decode a nested JSON struct with Swift Decodable protocol?

烂漫一生 提交于 2019-11-26 15:01:24
Here is my JSON { "id": 1, "user": { "user_name": "Tester", "real_info": { "full_name":"Jon Doe" } }, "reviews_count": [ { "count": 4 } ] } Here is the structure I want it saved to (incomplete) struct ServerResponse: Decodable { var id: String var username: String var fullName: String var reviewCount: Int enum CodingKeys: String, CodingKey { case id, // How do i get nested values? } } I have looked at Apple's Documentation on decoding nested structs, but I still do not understand how to do the different levels of the JSON properly. Any help will be much appreciated. Another approach is to