decodable

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

Dynamic JSON Decoding Swift 4

爷,独闯天下 提交于 2019-11-27 02:28:01
问题 I'm trying to decode the following JSON in Swift 4: { "token":"RdJY3RuB4BuFdq8pL36w", "permission":"accounts, users", "timout_in":600, "issuer": "Some Corp", "display_name":"John Doe", "device_id":"uuid824fd3c3-0f69-4ee1-979a-e8ab25558421" } The problem is, the last 2 elements ( display_name and device_id ) in the JSON may or may not exist or the elements could be named something entirely different but still unknown, i.e "fred": "worker", "hours" : 8 So what I'm trying to achieve is decode

How to parse JSON with Decodable protocol when property types might change from Int to String?

淺唱寂寞╮ 提交于 2019-11-26 18:30:16
问题 I have to decode a JSON with a big structure and a lot of nested arrays. I have reproduced the structure in my UserModel file, and it works, except with one property (postcode) that is in a nested array (Location) that sometimes is an Int and some other is a String. I don't know how to handle this situation and tried a lot of different solutions. The last one I've tried is from this blog https://agostini.tech/2017/11/12/swift-4-codable-in-real-life-part-2/ And it suggests using generics. But

How to use Any in Codable Type

拜拜、爱过 提交于 2019-11-26 13:56:31
问题 I'm currently working with Codable types in my project and facing an issue. struct Person: Codable { var id: Any } id in the above code could be either a String or an Int . This is the reason id is of type Any . I know that Any is not Codable . What I need to know is how can I make it work. 回答1: Codable needs to know the type to cast to. Firstly I would try to address the issue of not knowing the type, see if you can fix that and make it simpler. Otherwise the only way I can think of solving

Swift 4 JSON Decodable with multidimensional and multitype array

我与影子孤独终老i 提交于 2019-11-26 11:37:08
问题 { \"values\":[ [1,1,7,\"Azuan Child\",\"Anak Azuan\",\"12345\",\"ACTIVE\",\"Morning\",7,12,\"2017-11-09 19:45:00\"], [28,1,0,\"Azuan Child2\",\"Amran\",\"123456\",\"ACTIVE\",\"Evening\",1,29,\"2017-11-09 19:45:00\"] ] } Ok, this is my json format that i received from the server Right now i want to decode it into my struct but still have no luck on it. struct ChildrenTable: Decodable { var values: [[String]]? } And my caller method on URLSession look like this URLSession.shared.dataTask(with:

Swift 4 Decodable with keys not known until decoding time

孤人 提交于 2019-11-25 23:46:55
问题 How does the Swift 4 Decodable protocol cope with a dictionary containing a key whose name is not known until runtime? For example: [ { \"categoryName\": \"Trending\", \"Trending\": [ { \"category\": \"Trending\", \"trailerPrice\": \"\", \"isFavourit\": null, \"isWatchlist\": null } ] }, { \"categoryName\": \"Comedy\", \"Comedy\": [ { \"category\": \"Comedy\", \"trailerPrice\": \"\", \"isFavourit\": null, \"isWatchlist\": null } ] } ] Here we have an array of dictionaries; the first has keys