tristanhimmelman / ObjectMapper

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

Array of Dictionary mapping #1118

Open sam961 opened 3 years ago

sam961 commented 3 years ago

I have the below model and i need to map the array of object directly to the model without creating two models

one for 'childActivityList' and the other for nested array object

can i take the values directly?

childId <- map["childActivityList.0.childId"]

The above will always take the first value

In other words...i need to get rid of childActivityList

Your JSON dictionary:

  "data": {
        "childActivityList": [
            {
                "childId": "34bad8a5-0315-4d9f-a948-4780ed266040",
                "childName": "Test Produkte",
                "childImage": "https://testurl/s.png",
                "appName": "TikTok Lite",
                "appPackageName": "com.zhiliaoapp.musically.go",
                "appTime": "2167120",
                "openTime": "1619012403405",
                "deviceName": "Test Produkte_Android Tablet",
                "appIcon": null,
                "location": "Tirana",
                "isBlocked": false,
                "message": "now active on TikTok Lite"
            }
}

Your model:

class ActivityModel: Mappable, Encodable {

    var appName : String?
    var appPackageName : String?
    var appTime : String?
    var appIconURL:String?
    var deviceName : String?
    var id : String?
    var location : String?
    var openTime : String?
    var profileImage : String?
    var childName : String?
    var childImage : String?
    var message : String?

    required init?(map: Map) {

    }

    func mapping(map: Map) {

        appName <- map["appName"]
        appPackageName <- map["appPackageName"]
        appTime <- map["appTime"]
        deviceName <- map["deviceName"]
        id <- map["id"]
        location <- map["location"]
        openTime <- map["openTime"]
        profileImage <- map["profileImage"]
        appIconURL <- map["appIcon"]
        childName <- map["childName"]
}

What you did:

let activities = baseModel?.data?.childActivityList

What you expected:

I exepected something like:

Correct Model

What you got:

nil values