c# - 如何从 URL 获取 JSON 字符串?

我正在将我的代码从 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");

请记住,WebClientIDisposable,因此您可能会在生产代码中为此添加 using 语句。这看起来像:

using (WebClient wc = new WebClient())
{
   var json = wc.DownloadString("url");
}

https://stackoverflow.com/questions/5566942/

相关文章:

ruby - 如何将 JSON 转换为 Ruby 哈希

javascript - 如何从 JavaScript 中的 URL 获取 JSON?

javascript - node.js TypeError : path must be abso

javascript - 使用 Javascript 将 XML 转换为 JSON(并返回)

c# - json.net 有关键方法吗?

javascript - 如何测试字符串是否为 JSON?

json - postman :发送嵌套的 JSON 对象

javascript - JSON.stringify 以 pretty-print 方式输出到 d

javascript - 在 Node.js 中,如何将字符串转换为 json?

json - 将 curl POST 与 bash 脚本函数中定义的变量一起使用