decodable

Decodable for JSON with two structs under the same tag

假如想象 提交于 2021-02-17 05:26:05
问题 I have this json: { "stuff": [ { "type":"car", "object":{ "a":66, "b":66, "c":66 }}, { "type":"house", "object":{ "d":66, "e":66, "f":66 }}, { "type":"car", "object":{ "a":66, "b":66, "c":66 }} ]} As you can see for " car " and " house " there are different "object" structs, but both under the tag "object". It would be ideal if one ended up with something like struct StuffItem: Decodable { let type: TheType let car: Car let house: House } Is there some Codable, swifty, way to handle this? 回答1

How to parse Nested JSON Array into Codable struct

杀马特。学长 韩版系。学妹 提交于 2021-02-05 10:44:25
问题 I want to parse the JSON nested array into my decodable struct but the issue is there is no Key values how can i set into my decodable struct . I am also using SwiftJSON to parse object in my project... Here is my Decodable struct: struct PlayerData: Decodable { let playerID: Int? let platIDRef: Int? let EnrolledDateTime: String? let PlayerType: String? let LastCheckIn: Double? let SerialNo: Int? let Offered: Double? let PlayerReferralCode: Int? let PlayerStats: Double? let PlayerUpstream:

Swift JSON decoding with dependent types

淺唱寂寞╮ 提交于 2021-01-29 13:43:34
问题 I'm trying to decode "dependent" JSON API responses in Swift. Let's imagine a fictional API with two endpoints: /players , returns an array of objects with following attributes: id , an integer representing the player ID name , a string representing the player name /games , returns an array of objects with following attributes: name , a string representing the name of the game playerId1 , an integer representing the ID of the first player playerId2 , an integer representing the ID of the

Decode another response if first decoding failed using Combine and Swift

主宰稳场 提交于 2021-01-29 06:08:29
问题 I have the following model: struct Response: Decodable { let message: String } struct ErrorResponse: Decodable { let errorMessage: String } enum APIError: Error { case network(code: Int, description: String) case decoding(description: String) case api(description: String) } I'm trying to fetch an url and parse the JSON response using this flow: func fetch(url: URL) -> AnyPublisher<Response, APIError> { URLSession.shared.dataTaskPublisher(for: URLRequest(url: url)) // #1 URLRequest fails,

Decode URLs with special characters in Swift

社会主义新天地 提交于 2021-01-27 19:08:18
问题 An API I work with provides URL links, that can contain special characters like "http://es.dbpedia.org/resource/Análisis_de_datos" (the letter " á " inside). It's an absolutely valid URL, however, if a decodable class contains an optional URL? variable, it can't be decoded. I can change URL? to String? in my class and have a computed variable with something like URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) but perhaps there is a more elegant solution

Hot to decode JSON data that could and array or a single element in Swift?

自闭症网瘾萝莉.ら 提交于 2021-01-15 19:19:55
问题 I have a struct named Info which is decoded based on the data it receives. But sometimes, one of the values in data can either be a double or an array of double. How do I set up my struct for that? struct Info: Decodable { let author: String let title: String let tags: [Tags] let price: [Double] enum Tags: String, Decodable { case nonfiction case biography case fiction } } Based on the url, I either get price as a double { "author" : "Mark A", "title" : "The Great Deman", "tags" : [

Hot to decode JSON data that could and array or a single element in Swift?

╄→гoц情女王★ 提交于 2021-01-15 19:15:06
问题 I have a struct named Info which is decoded based on the data it receives. But sometimes, one of the values in data can either be a double or an array of double. How do I set up my struct for that? struct Info: Decodable { let author: String let title: String let tags: [Tags] let price: [Double] enum Tags: String, Decodable { case nonfiction case biography case fiction } } Based on the url, I either get price as a double { "author" : "Mark A", "title" : "The Great Deman", "tags" : [

Hot to decode JSON data that could and array or a single element in Swift?

早过忘川 提交于 2021-01-15 19:13:51
问题 I have a struct named Info which is decoded based on the data it receives. But sometimes, one of the values in data can either be a double or an array of double. How do I set up my struct for that? struct Info: Decodable { let author: String let title: String let tags: [Tags] let price: [Double] enum Tags: String, Decodable { case nonfiction case biography case fiction } } Based on the url, I either get price as a double { "author" : "Mark A", "title" : "The Great Deman", "tags" : [

Hot to decode JSON data that could and array or a single element in Swift?

丶灬走出姿态 提交于 2021-01-15 19:08:17
问题 I have a struct named Info which is decoded based on the data it receives. But sometimes, one of the values in data can either be a double or an array of double. How do I set up my struct for that? struct Info: Decodable { let author: String let title: String let tags: [Tags] let price: [Double] enum Tags: String, Decodable { case nonfiction case biography case fiction } } Based on the url, I either get price as a double { "author" : "Mark A", "title" : "The Great Deman", "tags" : [