JDS JDS - 1 month ago 8
reST (reStructuredText) Question

How do I read the Json object from a rest call?

This is my first exposure to working with the HttpClient.

I can see the status codes that come back from the rest call, but I'm unsure how can I read the json object that is returned from the GetData method?

public void MyTest()
{
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(_uri);

var response = httpClient.GetAsync("API/GetData");
}
}

Answer
public async Task MyTest() {
    using (HttpClient httpClient = new HttpClient()) {
        httpClient.BaseAddress = new Uri(_uri);

        var response = await httpClient.GetAsync("API/GetData");

        if(response!=null && response.IsSuccessStatusCode) {
            var json = await response.Content.ReadAsStringAsync();
        }
    }
}

the json variable will hold the string representation of the JSON. from there you can use it as needed.

also take a look at http://www.newtonsoft.com/json/help/html/deserializeobject.htm so that you can use strongly typed instances of the json