How to map this simple JSON using ObjectMapper


How to map this simple JSON using ObjectMapper



I have this simple JSON but I am not able to map it using ObjectMapper


{"response":404}



This is how I am doing it


import Foundation
import UIKit
import ObjectMapper

class YASEmail: Mappable {

var response: String

required init?(_ map: Map) {
response = ""
}

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



and this is how I am try to map


if let response = Mapper<YASEmail>().map(result) {
print(response)
}




2 Answers
2



I have updated your code. Please try this


import Foundation
import ObjectMapper

class YASEmail: Mappable {

var response : AnyObject
required init?(_ map: Map) {
response = ""

}

func mapping(map: Map) {
response = map.JSONDictionary["response"]!
}
}



And try to map like this:


if let response = Mapper<YASEmail>().map(jsonString) {
print(response.response)
}


/*
Copyright (c) 2018 Swift Models Generated from JSON powered by http://www.json4swift.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For support, please feel free to contact me at https://www.linkedin.com/in/syedabsar

*/

import Foundation
import ObjectMapper

struct Json4Swift_Base : Mappable {
var response : Int?

init?(map: Map) {

}

mutating func mapping(map: Map) {

response <- map["response"]
}

}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift