Ios 在swift中对动态键和动态对象使用Codable
我在服务调用返回的结构中嵌套了以下结构,但我无法对这部分进行编码/解码。我遇到的问题是customKey和customValue都是动态的。customValue可以是数组、字典或字典数组 比如说Ios 在swift中对动态键和动态对象使用Codable,ios,swift,swift5,Ios,Swift,Swift5,我在服务调用返回的结构中嵌套了以下结构,但我无法对这部分进行编码/解码。我遇到的问题是customKey和customValue都是动态的。customValue可以是数组、字典或字典数组 比如说 { "status": "a string value", "id": "a string value", "property": { "customkey1": "customValue1", "customKey2": [{
{
"status": "a string value",
"id": "a string value",
"property": {
"customkey1": "customValue1",
"customKey2": [{
"InnerCustomKey": "InnerCustomValue"
}, {
"InnerCustomKey": "InnerCustomValue"
}, {
"InnerCustomKey": "InnerCustomValue"
}],
"customkey3": {
"InnerCustomKey": "InnerCustomValue"
}
}
}
我尝试了var values:[String:String]之类的方法,但在另一个对象中失败了。
我遵循答案,但没有成功
extension KeyedDecodingContainerProtocol{
func getValueFromAvailableKey(codingKeys:[CodingKey])-> Any?{
for key in codingKeys{
for keyPath in self.allKeys{
if key.stringValue == keyPath.stringValue{
do{
if let value = try? self.decodeIfPresent([String].self, forKey:keyPath){
return value
}
if let value = try? self.decodeIfPresent([String:String].self, forKey:keyPath){
return value
}
if let value = try? self.decodeIfPresent([[String:String]].self, forKey:keyPath){
return value
}
if let value = try? self.decodeIfPresent(String.self, forKey:keyPath){
return value
}
return nil
}
}
}
}
return nil
}
}
private struct CustomCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
return nil
}
}
struct CustomModel: Codable {
var status:String?
var id: String?
var property: Property?
init(from decoder: Decoder) {
do{
let container = try decoder.container(keyedBy: CustomCodingKeys.self)
status = try container.decodeIfPresent(String.self, forKey: CustomCodingKeys.init(stringValue: "status")!)
id = try container.decodeIfPresent(String.self, forKey: CustomCodingKeys.init(stringValue: "id")!)
property = try container.decodeIfPresent(Property.self, forKey: CustomCodingKeys.init(stringValue: "property")!)
}catch{
print(error.localizedDescription)
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CustomCodingKeys.self)
try? container.encodeIfPresent(self.status, forKey: CustomCodingKeys.init(stringValue: "status")!)
try? container.encodeIfPresent(self.id, forKey: CustomCodingKeys.init(stringValue: "id")!)
try? container.encodeIfPresent(self.property, forKey: CustomCodingKeys.init(stringValue: "property")!)
}
}
// MARK: - Property
struct Property: Codable {
var customkey:[String:Any?] = [:]
init(from decoder: Decoder) {
do{
let container = try decoder.container(keyedBy: CustomCodingKeys.self)
for key in container.allKeys{
customkey[key.stringValue] = container.getValueFromAvailableKey(codingKeys: [CustomCodingKeys.init(stringValue: key.stringValue)!])
}
}catch{
print(error.localizedDescription)
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CustomCodingKeys.self)
for key in customkey{
try? container.encodeIfPresent(customkey[key.key] as? String, forKey: CustomCodingKeys.init(stringValue: key.key)!)
try? container.encodeIfPresent(customkey[key.key] as? [[String:String]], forKey: CustomCodingKeys.init(stringValue: key.key)!)
try? container.encodeIfPresent(customkey[key.key] as? [String:String], forKey: CustomCodingKeys.init(stringValue: key.key)!)
}
}
}
希望这对你有帮助
customValue
[reasonal]选项是否有限?是否所有可能的变量(字符串、数组、字典、字典数组)都是字符串类型的最里面的值?@user28434 customValue有任何值(字符串、数组、字典、字典数组等)您的示例代码很适合解码,但没有编码功能。您是否也可以添加编码代码?您希望如何编码???你想对同一个json编码吗?是的,我首先对你的样本完成的json字符串进行解码,然后添加一些数据,然后将这些数据编码为json。为此,我必须将财产的扩展作为encode@Mahesh我也重新编辑了编码的答案,