我从api检索到json有问题

时间:2018-05-17 11:22:06

标签: c# json api web

当我调用此API时: https://api.stackexchange.com/2.2/questions?order=desc&sort=creation&site=stackoverflow

我检索随机数据...... response

HttpClient client = new HttpClient();

ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
HttpWebRequest request = this.GetRequest("https://api.stackexchange.com/2.2/questions?order=desc&sort=creation&site=stackoverflow");
WebResponse webResponse = request.GetResponse();
var responseText = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();

任何人都可以提供帮助吗?

2 个答案:

答案 0 :(得分:0)

尝试以下代码行

using (WebClient wc = new WebClient())
{
   var json = wc.DownloadString("https://api.stackexchange.com/2.2/questions?order=desc&sort=creation&site=stackoverflow");
}

答案 1 :(得分:0)

阅读内容如下。您应该在流周围创建一个StreamReader,然后调用ReadToEnd。

using (Stream stream = webResponse.GetResponseStream())
{
   StreamReader reader = new StreamReader(stream, Encoding.UTF8);
   var responseText = reader.ReadToEnd();
}