decodable

Implement Codable for an Array of Dictionary with failable responses

一曲冷凌霜 提交于 2019-12-02 04:25:47
My JSON response is the following: { "data": [ { "unknown-key-c3e7f0": { "date_time": 1546944854000, "medication": "f4f25ea4-0607-4aac-b85a-edf40cc7d5b6", "record": { "status": "never" } }, "unknown-key-619d40": { "date_time": 1546944854000, "medication": "deef2278-f176-418f-ac34-c65fa54e712c", "record": { "status": "always" } }, "event": "06b445b9-dae0-48a1-85e4-b9f48c9a2349", "created": 1546949155020, "user": "8fb3fcd1-ffe6-4fd9-89b8-d22b1653cb6a", "id": "1546944855000", "type": "compliance" }, { "unknown-key-619d40": { "date_time": 1546944975000, "medication": "deef2278-f176-418f-ac34

Swift JSON Serialization typeMismatch

不羁岁月 提交于 2019-12-01 23:38:12
Currently struggling how to use Decodable. I've done some googling to the errors I'm getting but I still believe that the way i'm structuring the structs isn't correct but it seems to make sense to me. I've also tried using optionals In the error that I've posted at the end, I'm confused about the reference to the Double type. As I don't have any type or anything int he response that uses a double. (I'm also able to serialize the json reponse using the old swift method of casting the data as dictionaries - [String : Any]. But I'd like to use the modern/updated approach.) JSON Response {"NEWS":

Codable/Decodable should decode Array with Strings

一笑奈何 提交于 2019-12-01 18:48:56
Why is the names Array not decoding? Prepared for Playground, Simple paste this into your playground import Foundation struct Country : Decodable { enum CodingKeys : String, CodingKey { case names } var names : [String]? } extension Country { public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) names = try values.decode([String]?.self, forKey: .names)! } } let json = """ [{ "names": [ "Andorre", "Andorra", "アンドラ" ] },{ "names": [ "United Arab Emirates", "Vereinigte Arabische Emirate", "Émirats Arabes Unis", "Emiratos Árabes Unidos", "アラブ首長国連邦

Fatal error: Dictionary<String, Any> does not conform to Decodable because Any does not conform to Decodable

孤街浪徒 提交于 2019-12-01 17:39:41
I'm trying to use swift 4 to parse a local json file: { "success": true, "lastId": null, "hasMore": false, "foundEndpoint": "https://endpoint", "error": null } This is the function I'm using: func loadLocalJSON() { if let path = Bundle.main.path(forResource: "localJSON", ofType: "json") { let url = URL(fileURLWithPath: path) do { let data = try Data(contentsOf: url) let colors = try JSONDecoder().decode([String: Any].self, from: data) print(colors) } catch { print("Local JSON not loaded")} } } } but I keep getting the error: Fatal error: Dictionary does not conform to Decodable because Any

How to parse JSON using swift 4

纵饮孤独 提交于 2019-12-01 14:48:00
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.JSONData.com/fruits") URLSession.shared.dataTask(with: url!) { (data, response, error) in guard let data =

debugDescription: “Expected to decode Array<Any> but found a dictionary instead.”, underlyingError: nil)

佐手、 提交于 2019-12-01 11:17:02
I want to load an online json file into my application, but I am running into this error: typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil)) I have looked on stackoverflow but other sollutions didn't help to solve mine. My json My datamodel: import Foundation struct Initial: Codable { let copyright: String let totalItems: Int let totalEvents: Int let totalGames: Int let totalMatches: Int let wait: Int let dates: [Dates] } struct Dates: Codable { let date: String let

debugDescription: “Expected to decode Array<Any> but found a dictionary instead.”, underlyingError: nil)

北战南征 提交于 2019-12-01 09:30:21
问题 I want to load an online json file into my application, but I am running into this error: typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil)) I have looked on stackoverflow but other sollutions didn't help to solve mine. My json My datamodel: import Foundation struct Initial: Codable { let copyright: String let totalItems: Int let totalEvents: Int let totalGames: Int let

Parsing Nested JSON in SWIFT 4

前提是你 提交于 2019-12-01 04:15:05
问题 I am currently parsing JSON with Decodable in SWIFT 4. The JSON is formatted as follows: { "autopayout_from": "1.010", "earning_24_hours": "9.74731104", "error": false, "immature_earning": 0.635593030875, "last_payment_amount": "1.91238210", "last_payment_date": "Mon, 26 Feb 2018 15:08:02 GMT", "last_share_date": "Mon, 26 Feb 2018 16:16:01 GMT", "payout_daily": false, "payout_request": false, "total_hashrate": 109006.86, "total_hashrate_calculated": 143855.75, "transferring_to_balance": 0

Swift 4 Decodable - Additional Variables

点点圈 提交于 2019-12-01 03:40:50
Something I havent figured out or have been able to find online as of yet. Is there a way to add additional fields onto a struct containing the decodable protocol in which are not present in the JSON Data? For example and simplicity, say I have an array of json objects structured as such { "name": "name1", "url": "www.google.com/randomImage" } but say I want to add a UIImage variable to that struct containing the decodable such as struct Example1: Decodable { var name: String? var url: String? var urlImage: UIImage? //To add later } Is there a way to implement the decodable protocol in order

Decoding JSON array of different types in Swift

泪湿孤枕 提交于 2019-11-29 12:59:24
I'm trying to decode the following JSON Object { "result":[ { "rank":12, "user":{ "name":"bob","age":12 } }, { "1":[ { "name":"bob","age":12 }, { "name":"tim","age":13 }, { "name":"tony","age":12 }, { "name":"greg","age":13 } ] } ] } struct userObject { var name: String var age: Int } Basically a JSON Array with two different object types { "rank":12, "user": {userObject} } and a "1" : array of [userObjects] struct data: Decodable { rank: Int user: user 1: [user] <-- this is one area Im stuck } Thanks in advance Just for fun: First you need structs for the users and the representation of the