如何从 URL 获取 JSON 字符串?
我正在将代码从 XML 转换为 JSON。
但我找不到如何从给定的 URL 获取 JSON 字符串。
URL 是这样的:“https://api.facebook.com/method/fql.query?query=.....&format=json”
我之前使用过 XDocuments,在那里我可以使用 load 方法:
XDocument doc = XDocument.load("URL");
对于 JSON,此方法等效于什么?我正在使用 JSON.NET。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
System.Net
中使用WebClient
类:请记住,
WebClient
是IDisposable
,因此您可能会添加在生产代码中对此使用using
语句。这看起来像:Use the
WebClient
class inSystem.Net
:Keep in mind that
WebClient
isIDisposable
, so you would probably add ausing
statement to this in production code. This would look like:AFAIK JSON.Net 不提供从 URL 读取的功能。因此,您需要分两步执行此操作:
AFAIK JSON.Net does not provide functionality for reading from a URL. So you need to do this in two steps:
如果您使用的是 .NET 4.5 并且想要使用异步,那么您可以在
System.Net.Http
中使用HttpClient
:If you're using .NET 4.5 and want to use async then you can use
HttpClient
inSystem.Net.Http
: