c#间歇性Json反序列化获取cookie值的错误?

时间:2013-11-16 23:13:09

标签: c#-4.0 cookies json.net json-deserialization

问题:Json反序列化期间的偶然错误

我偶尔会在.cs文件中收到以下JsonReaderException:

  

解析值时遇到意外的字符:%。路径'',行   0,位置0。

在一行代码中,在这行代码中使用Newtonsoft.Json.JsonConvert 加载和反序列化JSON格式的cookie对象:

Dictionary<string, string> Z3Cookie = 
JsonConvert.DeserializeObject<Dictionary<string, string>>(MyCookinator.Get("MyZ3"));

来自Cookie的JSON值,根据Firebug = { “一个”: “4dSlshoOCgkg0zQop1cdZx41llyTzLlli1Ol19fNCK14dSlsh21pluss8qh74dSlshpluss85KnbQeq22eq22”, “B”: “9999”, “C”: “11”, “d”: “0”, “E”: “4dSlshoOCgkg0zQop1cdZx41llyTzLlli1Ol19fNCK14dSlsh21pluss8qh74dSlshpluss85KnbQeq22eq22”, “F”:“/ jtemplates / ratesn .html“,”g“:”1“,”h“:”1“,”i“:”0“,”j“:”0“,”k“:”0“,”l“:”0 “,”m“:”0“,”n“:”0“,”o“:”0“,”p“:”0“,”q“:”0“,”r“:”0“, “s”:“0”,“t”:“0”,“u”:“0”,“v”:“0”,“w”:“0”,“x”:“0”,“y” “:”0“,”z“:”0“}

MyCookinator函数获取cookie值=

public static string Get(string cookieName)
{
    var context = HttpContext.Current;
    Verify.That(context != null, "HttpContext is not available.");

    var responseCookie = GetCookie(context.Response.Cookies, cookieName);
    if (responseCookie != null)
    {
        return responseCookie.Value;
    }

    var requestCookie = GetCookie(context.Request.Cookies, cookieName);
    if (requestCookie != null)
    {
        return requestCookie.Value;
    }
     return null;
}

编辑:更多信息= GetCookie功能是:

private static HttpCookie GetCookie(HttpCookieCollection cookies, string key)
    {
        if (!cookies.AllKeys.Any(cookieKey => cookieKey == key))
        {
            return null;
        }

        return cookies[key];
    }

我不知道为什么我有时只会在这一行上收到错误,因为cookie的内容结构没有改变。

帮助..

我在c#.Net 4.0站点代码的各个方面使用Newtonsoft.Json没有任何问题,我希望使用Newtonsoft的反序列化器找到解决问题的方法。我错过了什么?

0 个答案:

没有答案
相关问题