gpt4 book ai didi

swift - 无法从解码器获取字典

转载 作者:行者123 更新时间:2023-11-30 11:44:26 25 4
gpt4 key购买 nike

我需要将整个字典作为键“内容”的字符串获取。我收到的它为零。我尝试使用 [String:Any] 抛出异常,任何内容都无法确认为可解码

public static func getModelObjects<R: Codable>(_ type: R.Type, from data: Any?) -> [T]? {
var mappedObjects: [T]?
if let dataAsDic = data as? JSONDictionary {
let decodedObject = decodableData(type, dictionaryData: dataAsDic)
mappedObjects = [decodedObject] as? [T]
} else if let dataAsArray = data as? [JSONDictionary] {
var objects: [R] = []
for dataAsDic in dataAsArray {
guard let decodedObject = decodableData(type, dictionaryData: dataAsDic) else {
return nil
}
objects.append(decodedObject)
}
mappedObjects = objects as? [T]
}
return mappedObjects
}

private static func decodableData<R: Codable>(_ type: R.Type, dictionaryData: JSONDictionary) -> R? {
let decoder = JSONDecoder()
do {
let jsonData = try JSONSerialization.data(withJSONObject: dictionaryData, options: JSONSerialization.WritingOptions.prettyPrinted)
let decodedData = try decoder.decode(type, from: jsonData)
return decodedData
} catch {
return nil
}
}

public class Configuration: CacheableEntity, ConfigurationStorageProtocol, Codable {
public dynamic var configurationID: String!

/// Version of configuration
public dynamic var version: String!

private var storedConfigDict: JSONDictionary?

private var storedConfigData: Data?
// Holds complete configuration as string

dynamic var configString: String?

/// Codable keys to confirm Codable protocol
private enum CodingKeys: String, CodingKey {
case configurationID = "id"
case version
case name
case content
}

public required init(from decoder: Decoder) throws {
super.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try? container.decode(String.self, forKey: .name)
self.configurationID = try? container.decode(String.self, forKey: .configurationID)
self.version = try? container.decode(String.self, forKey: .version)
self.configString = try? container.decode(String.self, forKey: .content)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try? container.encode(configurationID, forKey: .configurationID)
try? container.encode(version, forKey: .version)
try? container.encode(name, forKey: .name)
}
}

在下面的 json 中,我需要以字符串形式获取整个内容。我尝试过使用 [String:Any],但它崩溃了,因为任何内容都无法符合可解码的要求。

"name": "sdkconfig",
"id": "gma_5.0_ios_sdkconfig_1.0_us",
"version": "1.0",
"modifiedDateTime": "2017-06-15T08:15:25.304Z",
"content": {
"configName": "sdkConfig",
"configVersion": "1.0",
"enabled": {
"modules": [
"account",
"nutrition",
"restaurant",
"offer",
"ordering",
"favorite"
],
"socialAccounts": [
"facebook",
"google",
"twitter"
]
}
}

最佳答案

我不确定你的做法。但这就是我让它发挥作用的方式。像这样定义你的类。

    struct Configuration: Codable {

var content: String?
}

属性名称和json中的名称必须相同。 (在本例中为“内容”)。现在解析你的 json 字符串,如下所示。

    var configuration = JSONDecoder().decode(Configuration.self, from: "your json string")

关于swift - 无法从解码器获取字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48987275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com