带对象映射器的动态键

时间:2019-08-22 12:25:55

标签: ios json swift objectmapper

我正在用Swift 4编写,使用毒蛇结构和ObjectMapper将JSON响应映射到我的模型。

我正在尝试使用动态键映射这个相当复杂的JSON响应,并希望就我做错的事情获得一些反馈。

整个月上传的文档以月名称作为关键字返回,其所有文档列表均带有值。我的回应就像这个json:

{  
    "results": {
        "2019-08": [
            {
                "id": 2,
                "user_id": 7,
                "document": "1566282328atlassian-git-cheatsheet1.pdf",
                "name": "atoz",
                "order": 0,
                "is_edit": 0,
                "edit_json": "",
                "created_at": "2019-08-20 06:25:28",
                "updated_at": "2019-08-20 06:25:28",
                "date": "2019-08",
                "url": "http://35.154.206.145/storage/pdf/1566282328atlassian-git-cheatsheet1.pdf"
            },
        { ….}                
        ],
  "2019-07": [
            {
                "id": 2,
                "user_id": 7,
                "document": "1566282328atlassian-git-cheatsheet1.pdf",
                "name": "atoz",
                "order": 0,
                "is_edit": 0,
                "edit_json": "",
                "created_at": "2019-08-20 06:25:28",
                "updated_at": "2019-08-20 06:25:28",
                "date": "2019-08",
                "url": "http://35.154.206.145/storage/pdf/1566282328atlassian-git-cheatsheet1.pdf"
            },
       { ….}   
        ]
    }
}

我的模型类就是这样,以获取映射器模型类中的数据

import ObjectMapper

struct GroupResponse: Mappable {

    init?(map: Map) {}

    var results: [String: DocumentObject]?

    mutating func mapping(map: Map) {
        results   <- map["results"]
    }
}

class DocumentObject: Mappable{

    internal var months: [String: [DocumentListObject]]?

    required init?(map: Map) {}

    func mapping(map: Map) {
        for (monthKey, monthValue) in map.JSON as! [String: [String: Any]] {
            let month = DocumentListObject()
            months?[monthKey] = [month]
        }
    }
}

class DocumentListObject {

     var id:Int?
     var user_id:Int?
     var document:String?
     var name:String?
     var order:Int?
     var is_edit:Bool?
     var edit_json:String?
     var date:String?
     var url:String?
}

这有什么问题,我在api响应中添加错误并崩溃了

if let json = data as AnyObject? {
                let arrayResponse = json as! NSDictionary

                let arrayObject = Mapper<GroupResponse>().mapDictionary(JSON: arrayResponse as! [String : [String : Any]]) // I got crash here
                print(arrayObject)

1 个答案:

答案 0 :(得分:1)

不需要DocumentObject。试试这个,

struct GroupResponse: Mappable {

    init?(map: Map) {}

    var results: [String: [DocumentListObject]]?

    mutating func mapping(map: Map) {
        results   <- map["results"]
    }
}

此外,您忘记使DocumentListObject符合Mappable。请如下更新它,

class DocumentListObject: Mappable {

    var id:Int?
    var user_id:Int?
    var document:String?
    var name:String?
    var order:Int?
    var is_edit:Bool?
    var edit_json:String?
    var date:String?
    var url:String?

    required init?(map: Map) {}

    func mapping(map: Map) {
        id   <- map["id"]
        user_id   <- map["user_id"]
        document   <- map["document"]
        name   <- map["name"]
        order   <- map["order"]
        is_edit   <- map["is_edit"]
        edit_json   <- map["edit_json"]
        date   <- map["date"]
        url   <- map["url"]
    }
}

用法:

        let data = """
{
    "results": {
        "2019-08": [
            {
                "id": 2,
                "user_id": 7,
                "document": "1566282328atlassian-git-cheatsheet1.pdf",
                "name": "atoz",
                "order": 0,
                "is_edit": 0,
                "edit_json": "",
                "created_at": "2019-08-20 06:25:28",
                "updated_at": "2019-08-20 06:25:28",
                "date": "2019-08"
            }
          ]
       }
}
"""

        if let r = GroupResponse.init(JSONString: data), let result = r.results {
            for (key, value) in result {
                print("Key: \(key)" )
                print("DocumentName: \(value.first!.document!)")
            }
        }
// prints
Key: 2019-08
DocumentName: 1566282328atlassian-git-cheatsheet1.pdf

在响应中有JSON时,请使用下面的示例来解析GroupResponse。

let json = your JSON (of type [String: Any]) object retrieved from the API
if let r = GroupResponse.init(JSON: json), let result = r.results {
    for (key, value) in result {
        print("Key: \(key)" )
        print("DocumentName: \(value.first!.document!)")
    }
}