asp.net mvc - deserialize custom object returned from web api -
client make request web api method , object response problem cant desirialize object..
client method, making request web api
httpclient client = new httpclient(); client.baseaddress = new uri("http://localhost:57752"); httpresponsemessage response = client.getasync("api/auth/login/" + user.username + "/" + user.password).result; jsonresult result = null; if (response.issuccessstatuscode) { result = response.content.readasasync<jsonresult>().result; javascriptserializer json_serializer = new javascriptserializer(); user validuser = json_serializer.deserialize<user>(result.data.tostring());//throws exp. }
i want put object instance returned api validuser..
error message:
cannot convert object of type 'system.string' type 'mongodb.bson.objectid'
here models:
public abstract class entitybase { [bsonid] public objectid id { get; set; } } public class user : entitybase { //public string _id { get; set; } [required] [stringlength(100, errormessage = "the {0} must @ least {2} characters long.", minimumlength = 5)] [datatype(datatype.text)] [display(name = "username")] public string username { get; set; } [required] [stringlength(100, errormessage = "the {0} must @ least {2} characters long.", minimumlength = 6)] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; } public void encryptpassword() { password = encrypter.encode(this.password); } [datatype(datatype.password)] [display(name = "confirm password")] [compare("password", errormessage = "the password , confirmation password not match.")] public string confirmpassword { get; set; } }
you're not telling deserializer deserialize to. this
user validuser = (user)json_serializer.deserializeobject(result.data.tostring());
deserializes object , attempts cast object user
, going fail. need use generic method:
user validuser = json_serializer.deserialize<user>(result.data.tostring());
it's entirely possible need more work if json names , class names/struictures different changing property names serializing.
Comments
Post a Comment