1

我从 API 收到以下 json:

[ { "first_id": 1,
    "second_objs": [ { "second_id": 2,
                       "third_objs": [ { "third_id": 3,
                                         "param": "abcd" }] } ] } ]

并使用 ObjectMapper 对其进行序列化:

class First: NSObject, Mappable {
    var first_id: Int?
    var second_objs: [Second]?

    required init?(_ map: Map){}
    func mapping(map: Map) {
        first_id <- map["first_id"]
        second_objs <- map["second_objs"]
    }
}

class Second: NSObject, Mappable {
    var second_id: Int?
    var third_objs: [Third]?

    required init?(_ map: Map){}
    func mapping(map: Map) {
        second_id <- map["second_id"]
        third_objs <- map["third_objs"]
    }
}

class Third: NSObject, Mappable {
    var third_id: Int?
    var param: String?

    required init?(_ map: Map){}
    func mapping(map: Map) {
        third_id <- map["third_id"]
        param <- map["param"]
    }
}

现在,在收到并修改该 JSON 后,我需要将其发送回 API。但是,在发回之前,我需要将键“second_objs”和“third_objs”重命名为“something_else_2”和“something_else_3”。

[ { "first_id": 1,
    "something_else_2": [ { "second_id": 2,
                            "something_else_3": [ { "third_id": 3,
                                                    "param": "abcd" }] } ] } ]

如何以干净的方式进行重命名?

4

0 回答 0