我想在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它 –