jQuery parseJSON [英] jQuery parseJSON

查看:102
本文介绍了jQuery parseJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试解析一个JSON验证的字符串时,接收到此错误(JSON.parse:unexpected character)。当我删除需要转义的字符(style =width:400px;)时,它工作得很好。我缺少什么?在使用parseJSON之前是否有唯一的转义字符的方法?

  var $ cookieString ='{youTabItems:{youTab -001:< p style = \width:400px; \>欢迎使用我的测试< / 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 function eraseCookie(name){setCookie(name,,-1); }

var cookieObject = {youTabItems:{youTab-001:< p style = \width:400px; \>欢迎来到我的测试< / p& ,youTab-002:test02Value,youTab-003:test03Value}}
var cookieString = JSON.stringify($ cookieVal);
setCookie('youTabItems',cookieString,28);


解决方案

  var $ cookieString ='{youTabItems:{youTab-001:< p style = \\width:400px; \\>欢迎来到我的测试< / p& 002:test02Value,youTab-003:test03Value}}'; 

var $ myCookieString = $ .parseJSON($ cookieString);

将html作为字符串包装以使JSON有效。



为什么要使用双斜杠?



黑色斜杠是JavaScript字符串中的转义字符。这意味着我们必须自行转义,才能创建一个字符黑色斜杠。我们需要一个字符反斜杠作为JSON中的转义字符。



示例:

  var json ='{foo:\\}'; 

将创建字符串

  {foo:\} 

这是有效的JSON。如果我们只有一个反斜杠,它会创建

  {foo:} 


注意:

>这只是需要的,因为你的JSON是一个JavaScript字符串,如果你提供它作为一个HTTP响应,那么你只需要一个反斜杠,但无论你使用创建JSON将自动转义的引号,所以你不应该

更新



更好的方法来存储数据在cookie中是:

  var cookieObject = {youTabItems:{youTab-001:< p style = \width:400px; \>欢迎使用我的测试< / p>,youTab-002:test02Value,youTab-003:test03Value}}; 
var cookieString = JSON.stringify(cookieObject);


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);

解决方案

var $cookieString = '{"youTabItems": { "youTab-001": "<p style=\\"width:400px;\\">Welcome to my test</p>", "youTab-002": "test02Value", "youTab-003": "test03Value" }}';

var $myCookieString = $.parseJSON($cookieString);

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:

var json = '{"foo": "\\""}';

will create the string

{"foo": "\""}

which is valid JSON. If we only had one backslash, it would create

{"foo": """}

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:

var cookieObject = {"youTabItems": { "youTab-001": "<p style=\"width:400px;\">Welcome to my test</p>", "youTab-002": "test02Value", "youTab-003": "test03Value" }};
var cookieString = JSON.stringify(cookieObject);

这篇关于jQuery parseJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆