jsondecoder

JSON Decode does not work as expected swift 4

徘徊边缘 提交于 2020-03-17 03:21:11
问题 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

Swift Decodable, Endpoint returns completely different types

人盡茶涼 提交于 2020-02-06 08:03:56
问题 With API I'm working with, I have a case where 1 API Endpoint can return completely different responses, based on if the call was successful or not. In case of success , API Endpoint returns an Array of requested objects, in the root, something like this: [ { "key1": "value1", "key2": "value2", "key3": "value3" }, { "key1": "value1", "key2": "value2", "key3": "value3" }, ... ] which I'm normally decoding with try JSONDecoder().decode([Object].self, from: data) In case of an error , API

Swift Decodable, Endpoint returns completely different types

不想你离开。 提交于 2020-02-06 08:03:25
问题 With API I'm working with, I have a case where 1 API Endpoint can return completely different responses, based on if the call was successful or not. In case of success , API Endpoint returns an Array of requested objects, in the root, something like this: [ { "key1": "value1", "key2": "value2", "key3": "value3" }, { "key1": "value1", "key2": "value2", "key3": "value3" }, ... ] which I'm normally decoding with try JSONDecoder().decode([Object].self, from: data) In case of an error , API

How can I decode this json with Alamofire? [duplicate]

天大地大妈咪最大 提交于 2020-01-17 20:38:54
问题 This question already has answers here : How to parse a JSON file in swift? (17 answers) Closed 11 months ago . I want to print just value for key "User_number" [ { "User_fullname": null, "User_sheba": null, "User_modifiedAT": "2019-01-31T18:37:02.716Z", "_id": "5c53404e91fc822c80e75d23", "User_number": "9385969339", "User_code": "45VPMND" } ] 回答1: I suppose this is some JSON in Data format let data = Data(""" [ { "User_fullname": null, "User_sheba": null, "User_modifiedAT": "2019-01-31T18:37

How can I decode this json with Alamofire? [duplicate]

对着背影说爱祢 提交于 2020-01-17 20:34:58
问题 This question already has answers here : How to parse a JSON file in swift? (17 answers) Closed 11 months ago . I want to print just value for key "User_number" [ { "User_fullname": null, "User_sheba": null, "User_modifiedAT": "2019-01-31T18:37:02.716Z", "_id": "5c53404e91fc822c80e75d23", "User_number": "9385969339", "User_code": "45VPMND" } ] 回答1: I suppose this is some JSON in Data format let data = Data(""" [ { "User_fullname": null, "User_sheba": null, "User_modifiedAT": "2019-01-31T18:37

JSONDecoder unable to decode *value/fragment* types on *Mac*

狂风中的少年 提交于 2020-01-16 08:27:12
问题 Does anyone know of a way to configure the Mac version of JSONDecoder so that it is able to decode fragment/value types? I am writing Swift code that needs to work on both Mac and iOS. I am using Decodable with JSONDecoder to try and parse some JSON. I have an enum thats Decodable as follows: enum SomeEnum: Int, Decodable { case valueA = 0 case valueB = 1 } And to test the Decodable aspect of this I have this unit test... func test_problem() { let jsonData = "0".data(using: .utf8)! let result

Decoding a JSON array that has no field name

巧了我就是萌 提交于 2020-01-06 05:59:58
问题 I have a simple JSON file like this. { "january": [ { "name": "New Year's Day", "date": "2019-01-01T00:00:00-0500", "isNationalHoliday": true, "isRegionalHoliday": true, "isPublicHoliday": true, "isGovernmentHoliday": true }, { "name": "Martin Luther King Day", "date": "2019-01-21T00:00:00-0500", "isNationalHoliday": true, "isRegionalHoliday": true, "isPublicHoliday": true, "isGovernmentHoliday": true } ], "february": [ { "name": "Presidents' Day", "date": "2019-02-18T00:00:00-0500",

How to keep a flexible structure when using Codable in Swift

百般思念 提交于 2020-01-06 04:30:06
问题 I've got a API response structure, representing a user object, that looks like this: { "statuscode": 200, "response_type": 3, "errormessage": null, "detailresponse": { "id": "2", "shopifyautosync": null, "platformfeepercentage": null, "invited": null, "requiresyoutubesocialmediaupdate": 1, // Other properties ... } I'm using JSONDecoder().decode to decode to following structures: import Foundation class Response: Decodable { var statuscode: Int? var response_type: Int? // Other properties var

How to decode variable from json when key is changing according to user input?

放肆的年华 提交于 2020-01-06 03:43:05
问题 I am trying to parse some JSON response coming from CoinmarketCap using the JSONDecoder() in Swift 4. But the problem is that the response from json is changing according to user input. e.g if user wants the price in eur, the output is following: [ { "price_eur": "9022.9695444" } ] but if user wants the price in gbp: [ { "price_gbp": "7906.8032145" } ] So the question is how should I make the struct that inherits from Decodable if the variable(json key) name is changing? 回答1: You can decode

JSONDecodeError using Google Translate API with Python3

冷暖自知 提交于 2019-12-28 04:20:07
问题 I've searched thoroughly on Stack Overflow but couldn't find an answer to this problem. I'm trying to use the Google Translate API (googletrans 2.2.0) for Python (3.6.2) and am trying to translate a set of non-English documents into English. I am letting Google Translate do the language detection. Here is my code: ## newcorpus is a corpus I have created consisting of non-english documents fileids = newcorpus.fileids for f in fileids: p = newcorpus.raw(f) p = str(p[:15000]) translated_text =