tristanhimmelman / AlamofireObjectMapper

An Alamofire extension which converts JSON response data into swift objects using ObjectMapper
MIT License
2.66k stars 474 forks source link

I always got ObjectMapper failed to serialize response #246

Closed Dr1992 closed 5 years ago

Dr1992 commented 6 years ago

Hello dude, Please help me.

URL (GET) - "https://api.github.com/repos/ReactiveCocoa/ReactiveCocoa/pulls?state=all"

image

This is what i get when i print the responseString image

image ...

Mapper below

image

I always got
image

tiwari1amrit commented 6 years ago

Hello i have done in simple way.

class DiegoramosalmeidaModel{

class func getDiegoramosalmeida(){

    let url = "https://api.github.com/repos/ReactiveCocoa/ReactiveCocoa/pulls?state=all"

    Alamofire.request(url, method: .get).responseJSON { (response) in
        hideProgressHUD()
        let status = response.response?.statusCode
        print("STATUS \(status)")

        switch response.result{
        case .success(let value):
            print(value)

            if let val = value as? [[String : Any]]{

                let model = Mapper<PullRequest>().mapArray(JSONArray: val)
                print(model)

            }else{

                print("unable To Map")
            }

        case .failure(let error):
            print(error)
        }
    }
}

}

class PullRequest: Mappable {

var id:Int?
var title: String?
var description: String?
var url:String?
var root : Int?

var htmlUrl : String?
var diffUrl : String?
var patchUrl : String?

var user : User?

required init?(map: Map) {

}

func mapping(map: Map) {

    id <- map["id"]
    title <- map["title"]
    description <- map["description"]

    htmlUrl <- map["html_url"]
    diffUrl <- map["diff_url"]
    patchUrl <- map["patch_url"]

    user <- map["user"]
}

}

class User : Mappable{

var id:Int?
var login: String?
var avatarUrl: String?
var url:String?

required init?(map: Map) {

}

func mapping(map: Map) {

    id <- map["id"]
    login <- map["login"]
    avatarUrl <- map["avatar_url"]
    url <- map["url"]

}

}

tiwari1amrit commented 6 years ago

If any problem fill free to ask me.