WebUtility.HtmlDecode无法解码字符串

时间:2012-03-15 00:54:55

标签: c# json

我正在使用WebUtility.HtmlDecode尝试解码字符串,其中一部分位于以下位置:

checkin=%7B%22id%22%3A%224f612730e4b071dd72e3749d%22%2C%22createdAt%22%3A1331767088%2C%22type%22%3A%22checkin%22%2C%22timeZone%22%3A%22America%5C%2FDenver%22%2C

但是,WebUtility.HtmlDecode无法解码它,任何一个。如果它很重要,这就是我生成字符串的方式:

public static Dictionary<String, dynamic> DeserializeRequestStream(Stream requestStream, int contentLength)
        {
            requestStream.Position = 0; //Since a custom route with post data AND an ID passed in a parameter reads this stream. Damn bugs.
            var bytes = new byte[contentLength];
            requestStream.Read(bytes, 0, contentLength);    //(int)requestStream.Length - contentLength
            var jsonResponse = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
            var decodedResponse =  WebUtility.HtmlEncode(jsonResponse);
           //...snip...
        }

这应该是编码的JSON数据,我正在从POST请求中读取。我如何将此字符串解码为常规JSON?

编辑:Lasse V. Karlsen指出这实际上是URL编码的,而且我一直在咆哮错误的树。问题仍然存在:我将如何解码?

2 个答案:

答案 0 :(得分:6)

最简单的方法是将HtmlDecode更改为UrlDecode

原因:输入字符串checkin=%7B%22id%22%3A...是URL编码的,而不是HTML编码的。

答案 1 :(得分:1)

HttpServerUtility.UrlDecode方法(字符串)

http://msdn.microsoft.com/en-us/library/6196h3wt.aspx

相关问题