[英]JSONDecoder returning nil while parsing
我有以下 function: 其調用如下: 但出於某種原因,我的“歡迎”值總是返回零。 任何人都可以建議可能出了什么問題嗎? 當我print(json)時,出於某種原因我得到“294 字節”,很明顯在解碼之前出了點問題,對吧? 編輯:根據 Udi 的要求,這里是Welcome結構 這是 JSON ...
[英]JSONDecoder returning nil while parsing
我有以下 function: 其調用如下: 但出於某種原因,我的“歡迎”值總是返回零。 任何人都可以建議可能出了什么問題嗎? 當我print(json)時,出於某種原因我得到“294 字節”,很明顯在解碼之前出了點問題,對吧? 編輯:根據 Udi 的要求,這里是Welcome結構 這是 JSON ...
[英]Unable to parse JSON data properly from Alomafire
我知道對此有各種討論,但我對 Swift 中對象的理解似乎存在根本性缺陷。 所以我有以下 function 按預期返回 JSON 響應: 然后我能夠利用完成處理程序讀取 AL 的 scope 之外的 JSON 響應: 但我無法只獲取 JSON 的“capturedLists”部分。我需要捕獲的 JS ...
[英]Not able to parse JSON data from text file using python script
我有一個“.txt”文件,其中包含如下所示的 JSON 數據。[{'tradable': True, 'mode': 'full', 'instrument_token': 4708097, 'last_price': 178.65, 'last_traded_quantity': 5, 'aver ...
[英]Clarifications on JSONDecoder when decoding a single value
我試圖對JSONDecoder執行一些測試,但遇到了一個奇怪的行為。 特別是,當我使用以下代碼時會引發錯誤。 dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: “給定的數據無效 JSON。”, u ...
[英]How to extract and access data from JSON with PHP when some data changes?
我試圖將一些 API 數據引入 wordpress 中的目錄。我試圖獲取的數據只是加密貨幣價格,沒有其他信息,但因為它的格式有點嵌套(?)它似乎不起作用。 到目前為止,這是我的代碼: 結果是: 在這個例子中,我只是在 16833.02 之后嘗試對很多不同的硬幣進行此操作,“美元”將始終相同,但“比 ...
[英]Decoding json message inside of string
我有以下解碼 function;def flatten_data(json_data): """ Arguments: json_data (dict): json data Returns: dict : {a:1, b:2, b_c:1, ...
[英]Swift json decoder with mixed array
尋求幫助解碼不規則(因為缺少更好的詞)json。例如: 我可以解碼comment和前兩個“文本”: 但是第三個“文本”塊出現錯誤: “預期解碼 String 但發現了一個數組。” 據我了解,因為現在不是[String:String]的數組,而是[String:[String:String]和[St ...
[英]KeyedDecodingContainer not being called which is in a different package
下面的代碼在 swift package 管理器中,它處理 JSON 解碼器。 //MyJSONPackage extension KeyedDecodingContainer { func func decode(_ type: Bool.Type, forKey key: KeyedDe ...
[英]How to use sink to assign to an array
在演示中我切換到使用 Combine,但似乎無法找到一種方法來分配我從接收器中的 json 解碼中獲得的值以指向一個數組,下面是我的代碼,正如您在注釋掉的代碼中看到的那樣使用 URLSession 就容易多了……謝謝目前我只看到默認記錄 ...
[英]How to decode nested Json with Swift?
我一直在嘗試解碼這個 Json 數據,但我無法完全做到:這是我的樣本 json 數據:{ "id": 10644, "name": "CP2500", "numberOfConnectors": 2, "connectors": [ { "id": 59985, ...
[英]How to decode JSON in Swift?
我有一個 API JSON 的回復,如下所示。 我想解碼 JSON 以獲得字典數組[String:Double] ,例如[{"2020-01-01": 0.891186}, {"2020-01-02": 0.891186}] 。 我寫了如下解碼代碼: 我試圖定義一個結構: 但是我不知道應該在str ...
[英]How to access variable used when parsing JSON in another function?
我正在嘗試使用從 API 取回的文本並將其放入 UILabel 中,但我想不出將其放入全局 scope 中的方法。 這就是 VC 的樣子(注意我是 swift 的新手): 錯誤在上面的configureRandomText方法中(參見代碼中的注釋)。 ...
[英]Can't seem to decode JSON
我知道這類問題似乎得到了很多回答,但我似乎真的做不到。 我正在嘗試將一些 JSON 數據解碼到我的數據結構中。 我認為問題就在那里。 我的數據 model 可能有誤,但無法完全解決。 數據不是數組,里面有一個數組。 它試圖將字典解碼為數組,但是當我嘗試將我的結果變量初始化為數組以外的東西時,它不會 ...
[英]Swift compute field in struct on decode
我正在開發一個應用程序,該應用程序基於使用 JSONDecoder() 加載到結構中的值進行大量實時計算。 我需要最小化運行時的計算,所以我想預先做一些計算。 對於這個問題,我創建了一個帶有一些虛構名稱的工作示例,僅用於示例目的這段代碼從 json 加載數據。 我的任務是使用來自一個部分的數據並根據 ...
[英]How do I (simply) "do something else" to the resulting struct after the JSONDecoder().decode does it's job?
很簡單,我有這個: 我當然會這樣做...... 想象一下,我正在解碼其中的幾百個,也許是在一個數組中。 很簡單,每一個都解碼后,我想運行一些代碼。 所以在概念上類似於.. 恐怕我完全不知道該怎么做,搜索了很多。 它是如何完成的? 作為一個單獨的步驟手動完成似乎很可惜。 ...
[英]Dictionary with Coding keys in Swift, Codale to String
我有有效的工作代碼,但我想知道是否有辦法讓它更簡單、更小。 我有一個自定義 class 響應,可以從 Json 或文本初始化(取決於服務器的響應) 我可以以某種方式將編碼鍵和初始化與字典結合起來嗎? 例如 self.responseP1 = response[.responseP1] 但 self. ...
[英]How to map a single value from a JSON dictionary to a property when using Swift Decodable
我有一個看起來像這樣的 JSON object:{ "name": "Acid Arrow", "school": { "name": "Evocation", "url": "http://www.dnd5eapi.co/api/magic-sch ...
[英]Decoding date value from a json and show it as custom string date format in swiftUI
我正在嘗試解碼在 json 內具有日期值的 json 是這樣的 這是我的 Json 的結構 model。 我的問題是如何解碼這些日期並以 Text() 格式顯示它們,例如“ Mon 11/22 ”格式。 謝謝你的幫助。 ...
[英]How to send tokens with url using URLSession and decode it in SwiftUI
我需要在我的 iOS 應用程序中使用 get url 發送令牌,並在我看來使用它的響應。 可以通過以下代碼獲取 api: } the above code works perfectly if one doesn't have tokens to send In my case I have t ...
[英]How do I handle errors within a struct when parsing JSON in Swift
我正在調用 API 然后使用下面的簡化代碼對其進行解碼 Response 是我的結構,如下所示 然而,在大多數情況下,這很好用,有時 API 返回一個 JSON,它沒有名為name的鍵,而是鍵是otherName - 因此我收到一個錯誤,說“keyNotFound”。 有沒有辦法可以在我的結構或解 ...