I would like to store a JSON Object in Cookie. Since version 0 cookie values are restrictive in allowed characters, it only allows url safe characters. That's why I encoded it in UTF-8. After encoding it, JSON characters are changed.
Cookie cookie = new Cookie("res", URLEncoder.encode(res, "UTF-8"));
cookie.setComment("comment");
cookie.setMaxAge(24*60*60);
cookie.setPath("/t/res");
response.addCookie(cookie);
JSON:
{
"evt": [{
"id": "2",
"qty": "2"
}, {
"id": "3",
"qty": "7"
}],
"exc": [{
"id": "2",
"qty": "3"
}, {
"id": "1",
"qty": "6"
}],
"qt": "15",
"ti": "067e61623b6f4ae2a1712470b63dff00",
"rm": {
"aci": "6",
"rt": "5"
}
}
This is how it is shown in cookie:
%7B%22evt%22%3A%5B%5D%2C%22exc%22%3A%5B%5D%2C%22qt%22%3A%221%22%2C%22ti%22%3A%22067e61623b6f4ae2a1712470b63dff00%22%2C%22rm%22%3A%7B%22aci%22%3A%226%22%2C%22rt%22%3A%225%22%7D%7D
I parse it in JS JSON.parse(jsn)
, I get an error Unexpected token o in JSON at position 1
Am I missing something or how exactly can I parse it?