如何从url获取一个json字符串? [英] How to get a json string from url?

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

问题描述

我将代码XML转换为JSON。



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



网址是这样的: https://api.facebook.com/method/fql.query?query=.....&format=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字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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