decodable

How to parse one block of JSON data instead of the entire JSON sequence

丶灬走出姿态 提交于 2019-12-20 05:59:24
问题 Below is the code that am using to parse a JSON api. The code works and i am able to parse all the JSON values into the struct below but i would like to know how to parse only the first block(?)/ array and store them in the struct so that i can perform operations on them. let jsonUrlString = "https://www.alphavantage.co/query?function=FX_INTRADAY&from_symbol=EUR&to_symbol=USD&interval=5min&apikey=demo" let urlObj = URL(string: jsonUrlString) URLSession.shared.dataTask(with: urlObj!) {(data,

Swift decodable with unknown coding key and value [duplicate]

左心房为你撑大大i 提交于 2019-12-20 04:19:26
问题 This question already has an answer here : How can I decode a JSON response with an unknown key in Swift? (1 answer) Closed 20 days ago . I want to decode the following object from the server {"USD":6385.74,"JPY":715249.73,"EUR":5582.36} but I want to use a decodable struct with unknown key and value.Is this possible? Regards, Spyros 回答1: You can try let res = try? JSONDecoder().decode([String:Double].self,from:data) print(res["USD"]) which will enable you to decode any key 回答2: When I work

Swift JSON Serialization typeMismatch

▼魔方 西西 提交于 2019-12-20 03:17:35
问题 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

JSONEncoder and PropertyListEncoder don't conform to Encoder?

ぐ巨炮叔叔 提交于 2019-12-20 02:06:59
问题 I am trying to write an Encoder / Decoder pair that wraps an given Encoder / Decoder . The goal of this wrapper is to successfully handle cyclic references (which neither JSONEncoder nor PropertyListEncoder do), and delegate the actual encoding/decoding to the wrappded encoder. I was surprised to learn that neither JSONEncoder nor PropertyListEncoder conform to Encoder and similarly, their Decoder counterparts don't conform to Decoder ! This seems bizarre. If these two types don't conform,

JSONEncoder and PropertyListEncoder don't conform to Encoder?

流过昼夜 提交于 2019-12-20 02:05:53
问题 I am trying to write an Encoder / Decoder pair that wraps an given Encoder / Decoder . The goal of this wrapper is to successfully handle cyclic references (which neither JSONEncoder nor PropertyListEncoder do), and delegate the actual encoding/decoding to the wrappded encoder. I was surprised to learn that neither JSONEncoder nor PropertyListEncoder conform to Encoder and similarly, their Decoder counterparts don't conform to Decoder ! This seems bizarre. If these two types don't conform,

JSON Parsing using Decodable

北慕城南 提交于 2019-12-19 11:36:12
问题 I am trying to parse the following JSON using decodable protocol. I am able to parse string value such as roomName . But I am not able to map/parse owners, admins, members keys correctly. For eg, using below code, I can able to parse if the values in owners/members are coming as an array. But in some cases, the response will come as a string value(see owners key in JSON), but I am not able to map string values. Note: Values of admins, members, owners can be string or array (see owners and

JSON Parsing using Decodable

て烟熏妆下的殇ゞ 提交于 2019-12-19 11:36:00
问题 I am trying to parse the following JSON using decodable protocol. I am able to parse string value such as roomName . But I am not able to map/parse owners, admins, members keys correctly. For eg, using below code, I can able to parse if the values in owners/members are coming as an array. But in some cases, the response will come as a string value(see owners key in JSON), but I am not able to map string values. Note: Values of admins, members, owners can be string or array (see owners and

Swift 4 Decodable - Additional Variables

浪子不回头ぞ 提交于 2019-12-19 05:23:41
问题 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

What is difference between optional and decodeIfPresent when using Decodable for JSON Parsing?

落花浮王杯 提交于 2019-12-17 10:38:25
问题 I am using Codable protocol from Swift 4 first time, I am not able to understand use of decodeIfPresent from Decodable . /// Decodes a value of the given type for the given key, if present. /// /// This method returns `nil` if the container does not have a value associated with `key`, or if the value is null. The difference between these states can be distinguished with a `contains(_:)` call. /// /// - parameter type: The type of value to decode. /// - parameter key: The key that the decoded

keyNotFound(CodingKeys(stringValue: “coord”, intValue: nil)

≯℡__Kan透↙ 提交于 2019-12-13 09:13:48
问题 I am building a small swift weather app using the openweatherAPI and I am running into some issues trying to parse the JSON. I have used the following function to parse the get and parse the json. Below is my weather data struct: struct WeatherData: Codable { let coord: Coord let weather: [Weather] let base: String let main: Main let visibility: Int let wind: Wind let clouds: Clouds let dt: Int let sys: Sys let id: Int let name: String let cod: Int } struct Clouds: Codable { let all: Int }