Deepak ravi
Deepak ravi

Reputation: 53

How to map full json object in swift?

How to map full response object into a key in swift?

class Response: Mappable {
    var id = String()
    var fullResponse = NSDictionary()

    required init?(map: Map){

    }


    func mapping(map: Map) {
        id <- map["id"]
    // don't know how to map the full json from the repose to the fullResponse key.
        fullResponse <- map // map returns empty
    }
}

I don't know how to map the entire json object to a key.

Upvotes: 0

Views: 1783

Answers (2)

Deepak ravi
Deepak ravi

Reputation: 53

Here i did to make a key within the map class, it has full response in it.

class Response: Mappable {
    var id = String()
    var name = String()
    var fullResponse = NSDictionary()

    required init?(map: Map){

    }


    func mapping(map: Map) {
        id <- map["id"]
        name <- map["name"]
        fullResponse <- map 
    }
}

On the response from the request

let jsonData = response.data
if let json = try? JSONSerialization.jsonObject(with: jsonData!, options: []) as? NSDictionary{
   response.result.value?.fullResponse = json!
}

I don't know it's a correct procedure to do it. But it does the trick.

Upvotes: 0

Bapu
Bapu

Reputation: 94

Create a FullResponse Class. and map like

class Response: Mappable {
var id : String?
var name : String?

required init?(map: Map){

}


func mapping(map: Map) {
    id <- map["id"]
fullResponse key.
    name <- map["name"] 
}

}

once you will get the response

let jsonData = response.data
let json = try? JSONSerialization.jsonObject(with: jsonData!, options: []) as? [String : Any]
let yourResponse: Response? = Mapper<Response>().map(JSON: json!!)

Upvotes: 1

Related Questions