2017-11-09 10 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"] 
} 

}

のようにマッピングし、ここで私は、マップクラス内のキーを作るためにやった、それはそれで完全な応答を持っています。要求

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

からの応答に

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

私はそれはそれを行うには正しい手順だか分かりません。しかしそれはトリックです。

関連する問題