0

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; }
}
Yuval Itzchakov
  • 136,303
  • 28
  • 230
  • 296
kol1991
  • 65
  • 2
  • 13
  • possible duplicate of [Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class](http://stackoverflow.com/questions/11126242/using-jsonconvert-deserializeobject-to-deserialize-json-to-a-c-sharp-poco-class) – MethodMan Aug 19 '15 at 17:04
  • *It does not give the dictionary ... Only error* What is the error? – Yuval Itzchakov Aug 19 '15 at 17:11
  • when i try var obj = JsonConvert.DeserializeObject(p.Result); I got null – kol1991 Aug 19 '15 at 17:18
  • Yeah, but what does the error say. – Yuval Itzchakov Aug 19 '15 at 17:18
  • Error when i try deserialize to dictionary: An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.DLL Additional information: Error reading string. Unexpected token: StartObject. Path 'result', line 1, position 26. – kol1991 Aug 19 '15 at 17:19

1 Answers1

1

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

Eser
  • 11,700
  • 1
  • 17
  • 31
  • Nice, it works, but I thought I can do this easier, becouse now I must create all time new RootObject when I change my "result" object. Thx it is really helpful – kol1991 Aug 19 '15 at 17:23
  • Why is not Guid? It is generate using Guid.NewGuid() method and It is standard guid. Wiki "GUIDs are usually stored as 128-bit values, and are commonly displayed as 32 hexadecimal digits with groups separated by hyphens, such as {21EC2020-3AEA-4069-A2DD-08002B30309D}. " – kol1991 Aug 19 '15 at 17:53
  • @kol1991 OK, declare it as `Guid?` and you'll see null for them. But if you declare them as string you'll get values.... – Eser Aug 19 '15 at 18:32