简体   繁体   English

为什么我无法使用JSON.parse()解析此JSON字符串

[英]Why I cant parse this JSON String with JSON.parse()

I have this JSON String: 我有这个JSON字符串:

[{'Datum': 1465689600, 'Anwesend': false},{'Datum': 1465603200, 'Anwesend': false},{'Datum': 1465516800, 'Anwesend': false},{'Datum': 1465430400, 'Anwesend': false},{'Datum': 1465344000, 'Anwesend': false},{'Datum': 1465257600, 'Anwesend': false},{'Datum': 1465171200, 'Anwesend': false}]

and I want to parse this String. 我想解析这个字符串。 I tried: 我试过了:

var res = $.parseJSON(anwesendstr);

and: 和:

var res = JSON.parse(anwesendstr);

But everytime I get a: 但是每次我得到一个:

SyntaxError: Unexpected token ' in JSON at position 2

Exception 例外

Can I anybody help me to fix it? 有人可以帮我修复它吗?

In JSON, strings and object property names must be enclosed in double quotes. 在JSON中,字符串和对象属性名称必须用双引号引起来。 Your strings are enclosed in single quotes. 您的字符串用单引号引起来。 Change it to: 更改为:

[{"Datum": 1465689600, "Anwesend": false},{"Datum": 1465603200, "Anwesend": false},{"Datum": 1465516800, "Anwesend": false},{"Datum": 1465430400, "Anwesend": false},{"Datum": 1465344000, "Anwesend": false},{"Datum": 1465257600, "Anwesend": false},{"Datum": 1465171200, "Anwesend": false}]

and it will work. 它会工作。

You should use a library function to create JSON, rather than constructing it by hand in your code, to avoid problems like this. 您应该使用库函数来创建JSON,而不是在代码中手动构造它,以避免此类问题。 A library will also correctly escape embedded quotes and slashes in the JSON. 库还将正确地转义JSON中的嵌入式引号和斜杠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM