jsondecoder

SWIFT struggling with value assignment to optional class variable [duplicate]

拟墨画扇 提交于 2021-01-29 18:21:17
问题 This question already has answers here : how to unwrap swift optionals within a struct (1 answer) What does “Fatal error: Unexpectedly found nil while unwrapping an Optional value” mean? (11 answers) Closed 8 months ago . as an old C programmer I am trying to get into Swift. I have already overcome many hurdles but for the following I am out of ideas. I am building a little home schooling app and want to store/retrieve the high scores to/from a JSON file. import Foundation struct HighScores:

Is it possible to read a JSON sub-object directly as a JSON DOM object or Data or some other generic format?

馋奶兔 提交于 2021-01-28 11:00:23
问题 Is there a JSON DOM that can read any JSON, regardless of type, and store it into something like a Dictionary hierarchy? Consider this JSON and code... var jsonData = """ { "type":"ListWrapper", "objectData": { "label":"SuperItems", "items":[ { "value":"First" }, { "value":"Second" }, { "value":"Third" }, { "value":"Fourth" } ] } } """.data(using:.utf8)! Here's the first part of what we're trying to do. (Note: This doesn't actually work.) class TestObject : Codable { var name:String? var

Parsing complex JSON where data and “column headers” are in separate arrays

感情迁移 提交于 2021-01-28 04:14:11
问题 I have the following JSON data I get from an API: {"datatable": {"data" : [ ["John", "Doe", "1990-01-01", "Chicago"], ["Jane", "Doe", "2000-01-01", "San Diego"] ], "columns": [ { "name": "First", "type": "String" }, { "name": "Last", "type": "String" }, { "name": "Birthday", "type": "Date" }, { "name": "City", "type": "String" } ]} } A later query could result the following: {"datatable": {"data" : [ ["Chicago", "Doe", "John", "1990-01-01"], ["San Diego", "Doe", "Jane", "2000-01-01"] ],

General strategy to decode type mismatch keys in JSON into nil when optional in Swift

时光毁灭记忆、已成空白 提交于 2020-11-29 21:27:07
问题 Here is my problem, when I receive some JSON, it happens that some values do not match the required type. I don't really mind, I'm only interested by the value when its type is correct. For instance, the following structure: struct Foo : Decodable { var bar : Int? } I'd like it to match these JSON: { "bar" : 42 } => foo.bar == 42 { "bar" : null } => foo.bar == nil { "bar" : "baz" } => foo.bar == nil Indeed I'm looking for an optional Int , so whenever it's an integer I want it, but when it's

Eliminating quicktype accessory methods when parsng JSON to Swift

纵然是瞬间 提交于 2020-08-10 20:12:26
问题 I've noticed that while Quicktype.io does a very good job of parsing JSON into SWIFt, occasionally it resorts to a lot of auxiliary functions and methods. For the following, it generated about 15 additional methods. Some of these are familiar such as NSNull, however, two are unfamiliar to me such as JSONAny and it seems like there ought to be a way around them. The JSONAny class for example, has about 12 functions in it and it is used to parse just one field that is not that important to me.

Swift JSONDecoder can't decode valid JSON with escape characters

你。 提交于 2020-04-14 08:45:09
问题 In playgrounds, the following code produces an error: import Foundation struct Model: Codable { let textBody: String enum CodingKeys: String, CodingKey { case textBody = "TextBody" } } let json = """ { "TextBody": "First Line\n\nLastLine" } """.data(using: .utf8)! let model = try! JSONDecoder().decode(Model.self, from: json) Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given

JSON Decode does not work as expected swift 4

核能气质少年 提交于 2020-03-17 03:21:53
问题 if I want to decode my JSON there is something weird happening. Here's the structs struct chatMessages : Codable { var message: [chatMessage] } struct chatMessage : Codable { var user: String var message: String var status: String var latitude: Double var longitude: Double init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) user = try values.decode(String.self, forKey: .user) message = try values.decode(String.self, forKey: .message) status = try

JSON Decode does not work as expected swift 4

无人久伴 提交于 2020-03-17 03:21:14
问题 if I want to decode my JSON there is something weird happening. Here's the structs struct chatMessages : Codable { var message: [chatMessage] } struct chatMessage : Codable { var user: String var message: String var status: String var latitude: Double var longitude: Double init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) user = try values.decode(String.self, forKey: .user) message = try values.decode(String.self, forKey: .message) status = try