简体   繁体   中英

Post to WebApi and get result HttpClient

I'm trying send post to my WebApi and next get and deserialize result to my object. Im sending post from Windows Phone 8.1. How to get object sended from my 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 I get this 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 get only "result" value from this json string?

Unfortunately, my last line:

var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(p.Result);

It does not give the dictionary ... Only error

For more info this is object from 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 Where { 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 a root object to deserialize

public class RootObject
{
    public bool success { get; set; }
    public TravelOutput result { get; set; }
    public bool unAuthorizedRequest { get; set; }
}

Now you can deserialize as

var result = JsonConvert.DeserializeObject<RootObject>(p.Result);

your list is result.result.output now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM