错误ASP.NET MVC解析JSON值? [英] Error parsing JSON values in ASP.NET MVC?

查看:991
本文介绍了错误ASP.NET MVC解析JSON值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用计算器的搜索API搜索的问题。

I am trying to use StackOverflow's search API to search questions.

我是用这个动作来完成解析:

I am using this action to perform the parsing :

public ActionResult StackExchange(string sq)
{
    string url = "http://api.stackoverflow.com/1.1/search?intitle=" + sq + "&order=desc";    
    var client = new WebClient();
    var response = client.DownloadString(new Uri(url));
    JObject o = JObject.Parse(response);// ERROR
    int total = (int)o["total"];
    return View(total);
}

下面是JSON网址我试图解析:

Here is the JSON url I am trying to parse:

<一个href=\"http://api.stackoverflow.com/1.1/search?intitle=asp.net%20custom%20404&order=desc\">http://api.stackoverflow.com/1.1/search?intitle=asp.net%20custom%20404&order=desc

我试图提取以下数据:

`"total": 3` , 
`"question_timeline_url": "/questions/10868557/timeline",`
`"title": "Asp.net custom 404 not working using Intelligencia rewriter"`

它给错误为: Newtonsoft.Json.JsonReaderException:意外在分析价值时遇到的字符。路径',0行,位置0。

Its giving error as : Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: . Path '', line 0, position 0.

什么都可以对异常的原因是什么?我用同样的方法更早,它工作得很好。

What can be the reason for the exception? I used the same method earlier and it worked fine.

请建议。

推荐答案

试试下面的办法。

使用的NuGet并引用JSON.NET包。我看你已经做到了这一点。

Use NuGet and reference the JSON.NET package. I see you've already done this.

撰写的请求,并得到响应。

Compose a request and get the response.

string url = "http://api.stackoverflow.com/1.1/search?intitle=test&order=desc";
var request = (HttpWebRequest) WebRequest.Create(url);
var response = request.GetResponse();

从堆栈交易所API收到的响应gzip压缩!首先,您需要将它解压缩,然后才能阅读JSON响应。这就是为什么你收到异常。

The response you receive from the Stack Exchange API is gzipped! You first need to unzip it before you can read the JSON response. This is why you are receiving exceptions.

让我们创造出了这一点的方法。 .NET为我们提供了方便GZipStream类型用于这一目的。

Let's create a method which does just that. .NET provides us with the handy GZipStream type for this purpose.

private string ExtractJsonResponse(WebResponse response)
{
    string json;
    using (var outStream = new MemoryStream())
    using (var zipStream = new GZipStream(response.GetResponseStream(),
        CompressionMode.Decompress))
   {
        zipStream.CopyTo(outStream);
        outStream.Seek(0, SeekOrigin.Begin);
        using (var reader = new StreamReader(outStream, Encoding.UTF8))
        {
            json = reader.ReadToEnd();
       }
    }
    return json;
}

现在可以从响应中提取JSON数据。

Now you can extract the JSON data from the response.

var json = ExtractJsonResponse(response);

现在可以解析返回的数据。

Now you can parse the returned data.

JObject o = JObject.Parse(json);
int total = (int)o["total"];

去年我写了一篇关于如何使用1.1​​堆栈交易所API版本工作博客文章。

Last year I wrote a blog post on how to work with the Stack Exchange API version 1.1.

<一个href=\"http://cgeers.com/2011/10/02/stack-exchange-api/\">http://cgeers.com/2011/10/02/stack-exchange-api/

PS:我会建议你使用已经在今年早些时候发布的API 2.0版本

PS: I would recommend you use version 2.0 of the API which has been released earlier this year.

https://api.stackexchange.com/docs

这篇关于错误ASP.NET MVC解析JSON值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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