简体   繁体   English

这个 JSON 语法有什么问题?

[英]Whats wrong with this JSON syntax?

Am trying this on my chrome debugger console, and am getting a SyntaxError ;我在我的 chrome 调试器控制台上尝试这个,并且得到一个SyntaxError

JSON.parse("[{"name":"gath","age":10}]");
>SyntaxError

What is the correct way of parsing the JSON string?解析 JSON 字符串的正确方法是什么? Please note this question is a follow up to my earlier one, which am yet to get an answer!请注意,这个问题是对我之前的问题的跟进,尚未得到答案!

You need to escape your double-quotes.您需要转义双引号。

JSON.parse("[{\"name\":\"gath\",\"age\":10}]");

or, for better readabilty, define the string with single quotes:或者,为了更好的可读性,用单引号定义字符串:

JSON.parse('[{"name":"gath","age":10}]');
JSON.parse("[{\"name\":\"gath\",\"age\":10}]");

You cant have double quotes inside double quotes双引号内不能有双引号

You need to escape the " or do JSON.parse('[{"name":"gath","age":10}]');您需要转义 " 或执行 JSON.parse('[{"name":"gath","age":10}]');

Enclose it in single quotes and it will parse correctly.将其括在单引号中,它将正确解析。

JSON.parse('[{"name":"gath","age":10}]');


Object
age: 10
name: "gath"
__proto__: Object

Replace代替

JSON.parse("[{"name":"gath","age":10}]");

With

JSON.parse('[{"name":"gath","age":10}]');

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

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