tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.16k stars 1.03k forks source link

Array of Array of Object not mapping #1064

Closed Adrift001 closed 5 years ago

Adrift001 commented 5 years ago

Your JSON dictionary:

{
    "approvers": [
        [
            {
                "userId": 3,
                "userName": "Joe",
                "headUrl": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1560678148301&di=7b48c9d15211af372146cca167898046&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171030%2F26ed195281334ba4b1752394b60eb29a.jpeg"
            },
            {
                "userId": 4,
                "userName": "Manolo",
                "headUrl": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1560678148300&di=4396ce27dfcc3818018f011356ba7881&imgtype=0&src=http%3A%2F%2Fm.360buyimg.com%2Fpop%2Fjfs%2Ft23434%2F230%2F1763906670%2F10667%2F55866a07%2F5b697898N78cd1466.jpg"
            },
            null
        ],
        [
            {
                "userId": 4,
                "userName": "Manolo",
                "headUrl": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1560678148300&di=4396ce27dfcc3818018f011356ba7881&imgtype=0&src=http%3A%2F%2Fm.360buyimg.com%2Fpop%2Fjfs%2Ft23434%2F230%2F1763906670%2F10667%2F55866a07%2F5b697898N78cd1466.jpg"
            },
            null,
            {
                "userId": 11,
                "userName": "啊啊啊啊",
                "headUrl": ""
            }
        ]
    ]
}

Your model:

class LeaveApproverModel: Mappable {

    var userId: Double = 0
    var userName = ""
    var headUrl = ""

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        userId <- map["userId"]
        userName <- map["userName"]
        headUrl <- map["headUrl"]
    }
}

class LeaveApproversModel: Mappable {

    var approvers: [[LeaveApproverModel]] = [[]]

    required init?(map: Map) {

    }

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

What you did:

let repo = Mapper<LeaveApproversModel>().map(myJSONDictionary)

What you expected:

I exepected something like:

{"approvers":[[LeaveApproversModel, LeaveApproversModel], [LeaveApproversModel, LeaveApproversModel]]}

What you got:

{"approvers":[[]]}
Adrift001 commented 5 years ago

reference #152