jQuery 解析 JSON
当我尝试解析 JSON 验证的字符串时收到此错误(“JSON.parse:意外字符”)。当我删除需要转义的字符(style =“width:400px;”)时,它完美地工作。我缺少什么?在使用 parseJSON 之前是否有一种独特的方法来转义字符?
var $cookieString = '{"youTabItems": { "youTab-001": <p style=\"width:400px;\">Welcome to my test</p>, "youTab-002": "test02Value", "youTab-003": "test03Value" }}';
var $myCookieString = $.parseJSON($cookieString);
logThis($myCookieString);
更新
我能够让大部分工作正常工作,直到我开始从 cookie 中保存/检索。现在,它在分号之后切断内容......对此有什么想法吗?我使用在 quirsmode.com 上找到的 3 个函数来实现 cookie 功能(如下所示)。
function setCookie(name, value, days) { var date, expires; if (days) { date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toGMTString(); } else { expires = ""; } document.cookie = name + "=" + value + expires + "; path=/"; }
function getCookie(name) { var nameEQ = name + "=", ca = document.cookie.split(';'), c, i; for (i = 0; i < ca.length; i++) { c = ca[i]; while (c.charAt(0) === ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); } } return null; }
function eraseCookie(name) { setCookie(name, "", -1); }
var cookieObject = {"youTabItems": { "youTab-001": "<p style=\"width:400px;\">Welcome to my test</p>", "youTab-002": "test02Value", "youTab-003": "test03Value" }};
var cookieString = JSON.stringify($cookieVal);
setCookie('youTabItems', cookieString, 28);
Receiving this error ("JSON.parse: unexpected character") when I try to parse a JSON validated string. It works perfectly when I remove the characters that need escaped (style="width:400px;"). What am I missing? Is there a unique way to escape characters before you use parseJSON?
var $cookieString = '{"youTabItems": { "youTab-001": <p style=\"width:400px;\">Welcome to my test</p>, "youTab-002": "test02Value", "youTab-003": "test03Value" }}';
var $myCookieString = $.parseJSON($cookieString);
logThis($myCookieString);
Update
I was able to get the majority of it working, until I started saving / retrieving from cookies. Right now, its cutting the content off after the semicolon...any thoughts on this? I'm using 3 functions I found on quirsmode.com for the cookie functionality (shown below).
function setCookie(name, value, days) { var date, expires; if (days) { date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toGMTString(); } else { expires = ""; } document.cookie = name + "=" + value + expires + "; path=/"; }
function getCookie(name) { var nameEQ = name + "=", ca = document.cookie.split(';'), c, i; for (i = 0; i < ca.length; i++) { c = ca[i]; while (c.charAt(0) === ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); } } return null; }
function eraseCookie(name) { setCookie(name, "", -1); }
var cookieObject = {"youTabItems": { "youTab-001": "<p style=\"width:400px;\">Welcome to my test</p>", "youTab-002": "test02Value", "youTab-003": "test03Value" }};
var cookieString = JSON.stringify($cookieVal);
setCookie('youTabItems', cookieString, 28);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 html 包装为字符串以使 JSON 有效。
为什么使用双斜杠?
黑斜杠是 JavaScript 字符串中的转义字符。这意味着我们必须转义它本身来创建一个字面黑斜杠。我们需要一个反斜杠作为 JSON 中的转义字符。
示例:
字符串
将创建有效 JSON 的 。如果我们只有一个反斜杠,则会创建
无效的反斜杠。
注意:这只是需要的,因为您的 JSON 位于 JavaScript 字符串内。如果您将其作为 HTTP 响应来提供,那么您只需要一个反斜杠。但无论您使用什么来创建 JSON,都会自动转义引号,因此您不必处理这个问题。
更新
将数据存储在 cookie 中的更好方法是:
Wrap your html as a string to make the JSON valid.
Why double slashes?
The blackslash is the escape character in JavaScript strings. That means we have to escape it itself to create a literal blackslash. And we need a literal backslash as escape character in the JSON.
Example:
will create the string
which is valid JSON. If we only had one backslash, it would create
which is not valid.
Notice: This is only needed because your JSON is inside a JavaScript string. If you serve it e.g. as a HTTP response, then you only need one backslash. But whatever you use to create the JSON will escape the quotes automatically, so you should not have to deal with that.
Update
A better method to store the data in a cookie would be:
第一项没有用引号引起来:
The first item is not enclosed in quotes:
它是一个字符串值吗?那么它必须被引用
is it a string value? then it must be quoted