c# - Post to WebApi and get result HttpClient -
i'm trying send post webapi , next , deserialize result object. im sending post windows phone 8.1. how object sended web api?
my constructor:
public mytravelspage() { task gettravels = this.gettravels(); } internal async task gettravels() { string uri = "http://localhost:6234/api/services/app/travel/getmytravels"; travelpositions travel = new travelpositions(); travel.userguid = useridentity.userid; string x = jsonconvert.serializeobject(travel); var stringcontent = ""; using (var client = new system.net.http.httpclient()) { // new code: client.baseaddress = new uri(uri); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); httpcontent content = new stringcontent(x, encoding.utf8); var response = await client.postasync(uri, new stringcontent(x, encoding.utf8, "application/json")); var p = response.content.readasstringasync(); var result = jsonconvert.deserializeobject<dictionary<string, string>>(p.result); } }
in p.result string:
"{\"success\":true,\"result\":{\"output\":[{\"id\":1,\"userguid\":\"334e5955-fe89-4a49-b8ab-f0b36575bd8d\",\"where\":\"bukowno\",\"formoney\":false,\"money\":\"\",\"peoplecount\":1,\"driverguid\":null,\"longitude\":-122.188606,\"latitude\":47.622482},{\"id\":2,\"userguid\":\"334e5955-fe89-4a49-b8ab-f0b36575bd8d\",\"where\":\"krakow\",\"formoney\":false,\"money\":\"\",\"peoplecount\":1,\"driverguid\":null,\"longitude\":-122.188606,\"latitude\":47.622482},{\"id\":3,\"userguid\":\"334e5955-fe89-4a49-b8ab-f0b36575bd8d\",\"where\":\"warszawa\",\"formoney\":true,\"money\":\"\",\"peoplecount\":2,\"driverguid\":null,\"longitude\":-122.188606,\"latitude\":47.622482},{\"id\":4,\"userguid\":\"334e5955-fe89-4a49-b8ab-f0b36575bd8d\",\"where\":\"zakopane\",\"formoney\":false,\"money\":\"\",\"peoplecount\":1,\"driverguid\":null,\"longitude\":-122.188606,\"latitude\":47.622482}]},\"error\":null,\"unauthorizedrequest\":false}"
how "result" value json string?
unfortunately, last line:
var result = jsonconvert.deserializeobject<dictionary<string, string>>(p.result);
it not give dictionary ... error
for more info object webapi
public class traveloutput { public list<travel> output { get; set; } } public class travel { public int id { get; set; } public guid userguid { get; set; } public string { get; set; } public bool formoney { get; set; } public string money { get; set; } public int peoplecount { get; set; } public guid? driverguid { get; set; } public double longitude { get; set; } public double latitude { get; set; } }
you need root object deserialize
public class rootobject { public bool success { get; set; } public traveloutput result { get; set; } public bool unauthorizedrequest { get; set; } }
now can deserialize as
var result = jsonconvert.deserializeobject<rootobject>(p.result);
your list result.result.output
now
Comments
Post a Comment