JSON.parse错误给出带双引号字符串的错误 [英] JSON.parse error is giving error with double quotes string

查看:137
本文介绍了JSON.parse错误给出带双引号字符串的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那是为什么

//Code
JSON.parse("{'name':'Khushal Khan'}");

导致此错误

//Resposnse
SyntaxError: Unexpected token '

这很完美

//Code
JSON.parse('{"name":"Khushal Khan"}');

输出:

//Response
Object {name: "Khushal Khan"}

推荐答案

问题是JSON字符串中使用的引号类型,而不是外部引号. JSON规范仅允许使用双引号引起来的字符串.不过,您可以使用任何一种引号将JSON字符串实际传递给parse()函数.

The problem is the type of quote used in your JSON string, not the outer quotes. The JSON specification only allows for double quoted strings. You could use either type of quote to actually pass your JSON string to the parse() function, though.

来自 JSON规范:

这篇关于JSON.parse错误给出带双引号字符串的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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