2017-11-09 5 views
0

전체 응답 개체를 신속하게 키에 매핑하는 방법은 무엇입니까?전체 json 개체를 신속하게 매핑하는 방법은 무엇입니까?

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 
    } 
} 

전체 json 개체를 키로 매핑하는 방법을 모르겠습니다.

답변

1

FullResponse 클래스를 만듭니다. 당신은 내가지도 클래스 내에서 키를 만들기 위해 한 응답 여기

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

응답 해 주셔서 감사합니다. –

+0

fullResponse <- map [ "yourfullResponseKey"] –

+0

전체 응답 키가 없습니다. 그 응답에서. Reposonse JSON을 매핑하기 위해 필요한 키와 하나의 분리 된 키에 대한 응답을 매핑하고 있습니다. –

0

를 얻을 것이다 번과

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"] 
} 

}

처럼지도, 그것은 전체 응답을 가지고있다. 요청의 응답에

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 
    } 
} 

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

나는 그것을 할 수있는 올바른 절차를 알고하지 않습니다. 그러나 그것은 속임수입니다.