How to get a JSON string from URL?
Use the WebClient
class in System.Net
:
var json = new WebClient().DownloadString("url");
Keep in mind that WebClient
is IDisposable
, so you would probably add a using
statement to this in production code. This would look like:
using (WebClient wc = new WebClient()){ var json = wc.DownloadString("url");}
AFAIK JSON.Net does not provide functionality for reading from a URL. So you need to do this in two steps:
using (var webClient = new System.Net.WebClient()) { var json = webClient.DownloadString(URL); // Now parse with JSON.Net}
If you're using .NET 4.5 and want to use async then you can use HttpClient
in System.Net.Http
:
using (var httpClient = new HttpClient()){ var json = await httpClient.GetStringAsync("url"); // Now parse with JSON.Net}