我得到错误,而解析cookie使用jquery创建在asp.net.It抛出错误在第3行:解析值时遇到意外字符:%。路径",行0,位置0.
HttpCookie MyCookie = Request.Cookies["cart"];
Response.Write(MyCookie.Value.ToString());
var myobjects = JsonConvert.DeserializeObject<CookieCart>(MyCookie.Value.ToString());
Cookie样本值:
[
{
"id": 1,
"thumbnail": "/cocosamples/images/cocopeat/branded/430041-0014_1_t.jpg",
"title": "cocopeat",
"url": "product.html",
"price": "$ 250.00",
"qty": 3
}
]
CookieCart类
public int ID { get; set; }
public string Thumbnail { get; set; }
public string Title { get; set; }
public string Url { get; set; }
public string Price { get; set; }
public int Qty { get; set; }
MyCookie值:
% 5 b % 7 b % 22 id % 22% 3 a1 % 2 c % 22缩略图% 3 22% % 22 http % 3 a % 2 f % 2 flocalhost % 3 a52781 % 2 fasvonline % 2 ftheme % 2 fimages % 2 fwomen % 2 fskirt % 2 f430041 - 0014 _1_t.jpg % 22% 2 c % 22标题% 3 22% % 22 inceptos % 20奥利奇% 20 hac % 20自由人% 22% 2 c % 22 url % 3 22% % 22 product.html % 22% 2 c % 22价格% 3 22% % 22% 24% 20250.00% 22% 2 c % 22数量% 22% 3 a10 % 7 d % 5 d
饼干购物车
您的cookie值已被编码。使用Server.UrlDecode
。此外,您的JSON返回多个CookieCart
项目,因为它是一个具有[]
的数组。您需要将对象反序列化为List<CookieCart>
或CookieCart[]
,如:
var myobjects = JsonConvert.DeserializeObject<List<CookieCart>>
(Server.UrlDecode(MyCookie.Value.ToString()));
,然后得到一个对象:
CookieCart singleItem = myobjects.FirstOrDefault();