Reputation: 49
I am getting error while parsing cookie created using jquery in asp.net.It throws error at line 3: Unexpected character encountered while parsing value: %. Path '', line 0, position 0.
HttpCookie MyCookie = Request.Cookies["cart"];
Response.Write(MyCookie.Value.ToString());
var myobjects = JsonConvert.DeserializeObject<CookieCart>(MyCookie.Value.ToString());
Sample Cookie Value:
[
{
"id": 1,
"thumbnail": "/cocosamples/images/cocopeat/branded/430041-0014_1_t.jpg",
"title": "cocopeat",
"url": "product.html",
"price": "$ 250.00",
"qty": 3
}
]
CookieCart Class
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 Value:
%5B%7B%22id%22%3A1%2C%22thumbnail%22%3A%22http%3A%2F%2Flocalhost%3A52781%2FASVOnline%2Ftheme%2Fimages%2Fwomen%2Fskirt%2F430041-0014_1_t.jpg%22%2C%22title%22%3A%22Inceptos%20orci%20hac%20libero%22%2C%22url%22%3A%22product.html%22%2C%22price%22%3A%22%24%20250.00%22%2C%22qty%22%3A10%7D%5D
Cookie cart
Upvotes: 0
Views: 1752
Reputation: 223247
Your cookie value is encoded. Use Server.UrlDecode
. Also your JSON is returning multiple CookieCart
items since it is an array having []
. You need to de-serialize your object to either List<CookieCart>
or CookieCart[]
like:
var myobjects = JsonConvert.DeserializeObject<List<CookieCart>>
(Server.UrlDecode(MyCookie.Value.ToString()));
and then to get a single object:
CookieCart singleItem = myobjects.FirstOrDefault();
Upvotes: 3