如何在 JSON Codable Swift 4 的结果中添加或删除对象(使用动态键)

How to Add or Remove an object in result of JSON Codable Swift 4 (with dynamic Keys)

我想使用 Codable 在 swift 中导入 JSON,通过添加或删除对象来修改对象,并将其导出到 JSON。

这里是我的结构

class GenericCodingKeys: CodingKey {
var stringValue: String
var intValue: Int?

required init?(stringValue: String) { self.stringValue = stringValue }
required init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
}

class ListSpecie : Codable {
var species: [String : Specie]

required init(from decoder: Decoder) throws
{
    let container = try decoder.container(keyedBy: GenericCodingKeys.self)
    self.species = [String: Specie]()
    for key in container.allKeys{
        let value = try container.decodeIfPresent(Specie.self, forKey: GenericCodingKeys(stringValue: key.stringValue)!)
        self.species[key.stringValue] = value
    }
}
}

class Specie : Codable {
var name : String?
var latinName : [String]?

enum CodingKeys: String, CodingKey {
    case name = "l"
    case latinName = "ll"
}
required init(from decoder: Decoder) throws
{
    let sValues = try decoder.container(keyedBy: CodingKeys.self)
    name = try sValues.decodeIfPresent(String.self, forKey: .name)
    latinName = try sValues.decodeIfPresent(Array<String>.self, forKey: .latinName)
}
func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encodeIfPresent(name, forKey: .name)
    try container.encodeIfPresent(latinName, forKey: .latinName)
}
}

这里是示例代码JSON

let myJson = """
       {
            "especeID1": {
            "l": "Ail",
            "ll": ["Allium sativum L.","Allium"]
            },
            "especeID2": {
            "l": "Artichaut",
            "ll": ["Cynara cardunculus"]
            }
        }
    """

    let jsonDATA = myJson.data(using: .utf8)!

    do{
        self.jsonResult = try JSONDecoder().decode(ListSpecie.self, from: jsonDATA)
    }catch{
        print(error.localizedDescription)
    }

在这里,我想在 jsonResult

上添加或删除 specie 对象
    for myspecie in (self.jsonResult?.species)! {
        print(myspecie.key + " " +  myspecie.value.name!)
    }

    // Encodage
    let encoder = JSONEncoder()
    let productJSON = try! encoder.encode(self.jsonResult?.species) 

    let jsonString = String(data: productJSON, encoding: .utf8)!

有人可以告诉我如何在我的 jsonResult 变量中添加或删除一个 specie 对象。

非常感谢你能给我带来的帮助。

首先你的代码太复杂了,大部分代码都是多余的

一个class(考虑一个结构)就足够了

class Specie : Codable {
    var name : String?
    var latinName : [String]?

    enum CodingKeys: String, CodingKey {
        case name = "l"
        case latinName = "ll"
    }
}

如果 namelatin name 应该出现在任何地方,请声明属性非可选(删除问号)。

并解码 JSON

self.jsonResult = try JSONDecoder().decode([String:Specie].self, from: jsonDATA)

jsonResult现在是一个字典([String:Specie]),你可以删除项目

self.jsonResult.removeValue(forKey: "especeID2")

或添加项目

let newSpecies = Specie()
newSpecies.name = "Potato"
newSpecies.latinName = ["Solanum tuberosum"]
self.jsonResult["especeID3"] = newSpecies

并对对象进行编码

let encoder = JSONEncoder()
let productJSON = try! encoder.encode(self.jsonResult)
let jsonString = String(data: productJSON, encoding: .utf8)!