Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse integer response always nil (e.g status = 200) the result is always becomes nil .. any help ? thanks #62

Closed
rjjohnpatrickcruz opened this issue Feb 12, 2015 · 4 comments

Comments

@rjjohnpatrickcruz
Copy link

parsing response integer response and the result is always "nil"
example: (status = 200)
result = nil (when I parse the result)

@tristanhimmelman
Copy link
Owner

Please provide some more info so we can help. Your code and the JSON string you are trying to parse would help

@rjjohnpatrickcruz
Copy link
Author

Hey,

here is the sample json string im trying to parse
{
"completed_in" = "0.0919";
message = "Login Successful";
profile = {
"current_coordinates" = "";
email = "juan@delacruz.com";
fname = Juan;
id = 83;
lname = "Dela Cruz";
mname = Gitna;
"mobile_no" = 09090909090;
photo = "";
};
"security_key" = "4DB585AD-F0D2-28AB-68A7-5F4C43A55051";
status = 1;
}

---- swift code (using object mapper)----
let user = Mapper().map(JSON!)

---- swift code (my object mapper classes) ----

class User: Mappable {

var completed_in: String?
var status: String?
var message: String?
var security_key: String?
var profile: Profile?

required init(){}

// MapperProtocol
func mapping(mapper: Map) {
    completed_in <= mapper["completed_in"]
    status <= mapper["username"]
    message <= mapper["message"]
    security_key <= mapper["security_key"]
    profile <= mapper["profile"]
}

}

class Profile : Mappable {

var id : Int?
var email: String?
//var salt: String?
var fname: String?
var lname: String?
var mname: String?
var mobile: String?
var address: String?
var photo: String?
var current_coordinates: String?

required init(){}

// MapperProtocol
func mapping(mapper: Map) {
    id <= mapper["id"]
    email <= mapper["email"]
    fname <= mapper["fname"]
    lname <= mapper["lname"]
    mname <= mapper["mname"]
    mobile <= mapper["mobile"]
    address <= mapper["address"]
    photo <= mapper["photo"]
    current_coordinates <= mapper["current_coordinates"]

}

}

@tristanhimmelman
Copy link
Owner

Hi, you're models look properly formed but your JSON is not. You can test if JSON is properly formed using online tools like: http://jsonformatter.curiousconcept.com/

I've reformatted your JSON properly below.

{
"completed_in" : 0.0919,
"message" : "Login Successful",
"profile" : {
    "current_coordinates" : "",
    "email" : "juan@delacruz.com",
    "fname" : "Juan",
    "id" : 83,
    "lname" : "Dela Cruz",
    "mname" : "Gitna",
    "mobile_no" : "09090909090",
    "photo" : ""
},
"security_key" : "4DB585AD-F0D2-28AB-68A7-5F4C43A55051",
"status" : 1
}

The other issue I see is that you need to pass in a type when you are creating a Mapper object.

Mapper<User>().map(string: JSONString)

Hope this helps

@rjjohnpatrickcruz
Copy link
Author

Hi thanks , i think i see the issue.. im using alamofire to request the json and the json response is not a valid json format ..
thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants