我想在Cookie中存儲JSON對象。由於版本0的Cookie值在允許的字符中受到限制,因此它只允許url安全字符。這就是爲什麼我用UTF-8編碼它的原因。編碼之後,JSON字符被改變。在Cookie中存儲JSON對象
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"
}
}
這是它是如何在餅乾所示:
%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
我解析它在JS JSON.parse(jsn)
,我得到一個錯誤Unexpected token o in JSON at position 1
上午我錯過了什麼或者我究竟可以解析它?
它只是返回'{}' –
參考下面的回答,認爲你可能需要做的你正在做什麼,但首先解碼該網址,然後JSON.parse它 –