問題: Json の逆シリアル化中に時折エラーが発生する
.cs ファイルで次の JsonReaderException を受け取ることがあります。
値の解析中に予期しない文字が検出されました: %。パス ''、行 0、位置 0。
次のコード行で、Newtonsoft.Json.JsonConvertを使用して、JSON 形式の Cookie オブジェクトを読み込んで逆シリアル化するコード行で:
Dictionary<string, string> Z3Cookie =
JsonConvert.DeserializeObject<Dictionary<string, string>>(MyCookinator.Get("MyZ3"));
The JSON value from cookie as per Firebug = {"a": "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"}
Cookie 値を取得する MyCookinator 関数 =
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 のデシリアライザーを使用して問題の解決策を探しています。何が欠けていますか?