[c#] How to get a json string from url?

I'm switching my code form XML to JSON.

But I can't find how to get a JSON string from a given url.

The URL is something like this: "https://api.facebook.com/method/fql.query?query=.....&format=json"

I used XDocuments before, there I could use the load method:

XDocument doc = XDocument.load("URL");

What is the equivalent of this method for JSON? I'm using JSON.NET.

This question is related to c# json facebook linq-to-xml webclient

The answer is


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
}

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?

Examples related to facebook

I am receiving warning in Facebook Application using PHP SDK React-Native: Application has not been registered error Can't Load URL: The domain of this URL isn't included in the app's domains Facebook OAuth "The domain of this URL isn't included in the app's domain" Facebook login message: "URL Blocked: This redirect failed because the redirect URI is not whitelisted in the app’s Client OAuth Settings." Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView` Open Facebook Page in Facebook App (if installed) on Android App not setup: This app is still in development mode IOS - How to segue programmatically using swift Get ALL User Friends Using Facebook Graph API - Android

Examples related to linq-to-xml

Uses for the '&quot;' entity in HTML Finding element in XDocument? how to use XPath with XDocument? How to get a json string from url? How to put attributes via XElement XDocument or XmlDocument Converting XDocument to XmlDocument and vice versa How to Get XML Node from XDocument Populate XDocument from String LINQ to read XML

Examples related to webclient

Deciding between HttpClient and WebClient "A connection attempt failed because the connected party did not properly respond after a period of time" using WebClient POSTing JSON to URL via WebClient in C# HttpClient does not exist in .net 4.0: what can I do? Sending HTTP POST with System.Net.WebClient How to get a json string from url? How to post data to specific URL using WebClient in C# What difference is there between WebClient and HTTPWebRequest classes in .NET? How to get status code from webclient? How do I authenticate a WebClient request?