我正在将我的代码从 XML 转换为 JSON。
但我找不到如何从给定的 URL 获取 JSON 字符串。
网址是这样的:“https://api.facebook.com/method/fql.query?query=.....&format=json”
我以前用过XDocuments,那里可以用load方法:
XDocument doc = XDocument.load("URL");
这个方法对于 JSON 的等价物是什么?我正在使用 JSON.NET。
最佳答案
使用System.Net
中的WebClient
类:
var json = new WebClient().DownloadString("url");
请记住,WebClient
是 IDisposable
,因此您可能会在生产代码中为此添加 using
语句。这看起来像:
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("url");
}
https://stackoverflow.com/questions/5566942/