问题描述
我将代码XML转换为JSON。但是我找不到如何从给定的URL获取JSON字符串。
网址是这样的:
我以前使用XDocuments,我可以使用加载方法:
XDocument doc = XDocument.load(URL);
JSON的这种方法相当于什么?我正在使用JSON.NET。
使用 WebClient
在 System.Net
:
var json = new WebClient()。 DownloadString( URL);
请记住, WebClient
是 IDisposable
,所以您可能会在生产代码中使用语句添加一个语句。这将是如下所示:
using(WebClient wc = new WebClient())
{
var json = wc.DownloadString(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.
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");
}
这篇关于如何从url获取一个json字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!