C# GET Request and Parsing JSON

31,972

You could use the HttpClient class. The GetAsync method allows you to send a GET request to a specified url:

public async Task<JsonObject> GetAsync(string uri)
{
    var httpClient = new HttpClient();
    var content = await httpClient.GetStringAsync(uri);
    return await Task.Run(() => JsonObject.Parse(content));
}
Share:
31,972
Ramesh
Author by

Ramesh

cool.

Updated on July 09, 2022

Comments

  • Ramesh
    Ramesh almost 2 years

    I am developing Windows store app in Windows 8, Visual Studio 2012. I need to make GET request to a particular URL and get the JSON as response. And I need to parse the JSON to get the values in it. I need C# code to do the above functionality.

  • Ramesh
    Ramesh about 11 years
    @Darin Dimitrov Hey, I have written code up to getting response. I need to know how to get JSON from response and parse the JSON.
  • musefan
    musefan about 11 years
    @Ramesh: So why not just ask a question about parsing? And say in your question that you already have the response string?
  • Darin Dimitrov
    Darin Dimitrov about 11 years
    @Ramesh, that's exactly what I have shown in my answer. Did you read it? You use the GetAsync method to send a GET request and then the JsonObject.Parse method to parse the returned JSON string.
  • Ramesh
    Ramesh about 11 years
    @musefan Actually I already had for POST. Just now figured out I can reuse for GET also.
  • Darin Dimitrov
    Darin Dimitrov about 11 years
    @musefan, answering a question depends on my mood. Now I am in a good mood, so I answer all kind of questions.
  • Simple
    Simple about 3 years
    @DarinDimitrov Visual Studio complains about the return type it should be a JsonValue instead of a JsonObject.
  • Simple
    Simple almost 3 years
    @DarinDimitrov This is bad code as it blocks a background thread, I reproduced your code and it blocked my application, see: ben-morris.com/…