I'm switching my code form XML to JSON.
我将代码从XML转换为JSON。
But I can't find how to get a JSON string from a given url.
但是我找不到如何从给定的url获取JSON字符串。
The URL is something like this: "https://api.facebook.com/method/fql.query?query=.....&format=json"
URL是这样的:" https://api.facebook.com/method/fqlquery? query. & formatjson "
I used XDocuments before, there I could use the load method:
我之前用过XDocuments,在那里我可以使用load方法:
XDocument doc = XDocument.load("URL");
What is the equivalent of this method for JSON? I'm using JSON.NET.
JSON对应的方法是什么?我使用JSON.NET。
3 个解决方案
#1
217
Use the WebClient
class in System.Net
:
在System.Net中使用WebClient类:
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:
请记住,WebClient是IDisposable,因此您可能会在产品代码中添加一条using语句。这样子:
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("url");
}
#2
90
AFAIK JSON.Net does not provide functionality for reading from a URL. So you need to do this in two steps:
JSON的。Net不提供从URL读取的功能。所以你需要分两个步骤来做:
using (var webClient = new System.Net.WebClient()) {
var json = webClient.DownloadString(URL);
// Now parse with JSON.Net
}
#3
34
If you're using .NET 4.5 and want to use async then you can use HttpClient
in System.Net.Http
:
如果您正在使用。net 4.5并且希望使用异步,那么您可以在System.Net.Http:
using (var httpClient = new HttpClient())
{
var json = await httpClient.GetStringAsync("url");
// Now parse with JSON.Net
}
#1
217
Use the WebClient
class in System.Net
:
在System.Net中使用WebClient类:
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:
请记住,WebClient是IDisposable,因此您可能会在产品代码中添加一条using语句。这样子:
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("url");
}
#2
90
AFAIK JSON.Net does not provide functionality for reading from a URL. So you need to do this in two steps:
JSON的。Net不提供从URL读取的功能。所以你需要分两个步骤来做:
using (var webClient = new System.Net.WebClient()) {
var json = webClient.DownloadString(URL);
// Now parse with JSON.Net
}
#3
34
If you're using .NET 4.5 and want to use async then you can use HttpClient
in System.Net.Http
:
如果您正在使用。net 4.5并且希望使用异步,那么您可以在System.Net.Http:
using (var httpClient = new HttpClient())
{
var json = await httpClient.GetStringAsync("url");
// Now parse with JSON.Net
}