如何从 URL 获取 JSON 字符串? [英] How to get a JSON string from URL?

查看:48
本文介绍了如何从 URL 获取 JSON 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的代码格式 XML 转换为 JSON.

I'm switching my code form XML to JSON.

但我找不到如何从给定的 URL 获取 JSON 字符串.

But I can't find how to get a JSON string from a given URL.

URL 是这样的:https://api.facebook.com/method/fql.query?query=.....&format=json"

The URL is something like this: "https://api.facebook.com/method/fql.query?query=.....&format=json"

我之前用过 XDocuments,在那里我可以使用 load 方法:

I used XDocuments before, there I could use the load method:

XDocument doc = XDocument.load("URL");

这个方法对于 JSON 的等价物是什么?我正在使用 JSON.NET.

What is the equivalent of this method for JSON? I'm using JSON.NET.

推荐答案

使用System.Net中的WebClient类:

var json = new WebClient().DownloadString("url");

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

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 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆