$.parseJSON giving "Uncaught SyntaxError: Unexpected token"

I'm parsing a json string with JQuery parseJSON

var jsontest = '[{"nombre":"Campau00f1a de prueba","parcelas":"10","stampCreacion":"2014-12-30 18:18:26","estado":"1","id":"1","active":"1","camposControl":[{"nombre":"Repeticiu00f3n","tipo":"2","id":"2","active":"1"},{"nombre":"Comentarios","tipo":"1","id":"3","active":"1"}]},{"nombre":"Campau00f1a2","parcelas":"10","stampCreacion":"2014-12-30 20:07:36","estado":"1","id":"2","active":"1","camposControl":[{"nombre":"Opciones","tipo":"3","id":"16","active":"1","opciones":[{"nombre":"muchasr","id":"12","active":"1"},{"nombre":"opcionesr","id":"13","active":"1"},{"nombre":"para mi r","id":"14","active":"1"},{"nombre":"y para ti","id":"15","active":"1"}]},{"nombre":"numerito por aqui","tipo":"2","id":"17","active":"1"}]}]';



var obj = $.parseJSON(jsontest);

It appears to be a valid json according to jsonlint, but as you may see in the fiddle it is giving the next error:

Uncaught SyntaxError: Unexpected token -> jquery-1.11.2.min.js:4

http://jsfiddle.net/6a9qLtq2/

I'm using chrome BTW


The errors I see are with your escapes ''. I replaced all of your uXXXX codes with uXXXX and the only additional errors I got were with your 'r'. I removed the 'r's as well and got this to work:

    var jsontest = '[{"nombre":"Campau00f1a de prueba","parcelas":"10","stampCreacion":"2014-12-30 18:18:26","estado":"1","id":"1","active":"1","camposControl":[{"nombre":"Repeticiu00f3n","tipo":"2","id":"2","active":"1"},{"nombre":"Comentarios","tipo":"1","id":"3","active":"1"}]},{"nombre":"Campau00f1a2","parcelas":"10","stampCreacion":"2014-12-30 20:07:36","estado":"1","id":"2","active":"1","camposControl":[{"nombre":"Opciones","tipo":"3","id":"16","active":"1","opciones":[{"nombre":"muchas","id":"12","active":"1"},{"nombre":"opciones","id":"13","active":"1"},{"nombre":"para mi ","id":"14","active":"1"},{"nombre":"y para ti","id":"15","active":"1"}]},{"nombre":"numerito por aqui","tipo":"2","id":"17","active":"1"}]}]';


var obj = $.parseJSON(jsontest);
console.log(obj);

I'm not sure what you were going for with the 'r's, but if you were going for carriage return I think you want 'n'.

As a note about escapes, you have to keep in mind that you have strings within a string, so you have to double escape (the reason why you need two backslashes instead of one).

Oh, and here's a fork http://jsfiddle.net/gok31tqu/

And then for testing, I did another fork where I replaced your 'r' with 'r' and it appears to run as well:

http://jsfiddle.net/9vktyj29/

Hope that helps.


Update JSFiddle to:

var jsonstringify = JSON.stringify(jsontest);
    var obj = $.parseJSON(jsonstringify);

to encode the backslashes


use eval(jsonData) will fix this issue..
链接地址: http://www.djcxy.com/p/45948.html

上一篇: console.log和JSON.parse返回错误

下一篇: $ .parseJSON给出“Uncaught SyntaxError:意外的标记”