0

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

3
  • how is your class CookieCart looks like? Commented Mar 27, 2014 at 17:24
  • when i try to read the same cookie value using JavaScriptSerializer i got error Invalid JSON primitive: . Commented Mar 27, 2014 at 18:36
  • you need Server.UrlDecode Commented Mar 27, 2014 at 18:42

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

4 Comments

Now i am getting the following error... Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CookieCart' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CookieCart' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
@padmaCybonyx, Fixed that in my answer. You are basically getting a collection of CookieCart. Check the updated code.
good catch...u saved me a day... had been trying from afternoon using various methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.