JSON.parse 中不允许使用哪些字符?
What characters arent allowed in a JSON.parse?
所以在收到我的回复后,我一直收到解析错误。有非法字符吗?
这是回复
[{"businessID": ChIJ49DlQ5NiwokRQ_noyKqlchQ,"latitude": 40.733038,"longitude":-73.6840691,"address":"1201
Jericho Turnpike, New Hyde Park","businessname":"SUBWAY®Restaurants"},{"businessID": ChIJZfl6R5NiwokRZo7PU4NPoMY
,"latitude": 40.7329359,"longitude":-73.684513,"address":"1113 Jericho Turnpike, New Hyde Park","businessname"
:"Gino's"},{"businessID": ChIJcbpnRJNiwokRrtbOKe7HQo0,"latitude": 40.733049,"longitude":-73.684006,"address"
:"1203 Jericho Turnpike, New Hyde Park","businessname":"Wong's Garden"},]
这是我处理响应的函数。我确定它在警报之前就中断了,因为警报没有被触发
var datad = $(msg).text();
console.log(datad);
var resultstring = datad.replace(',]',']');
var JsonParseData = JSON.parse(resultstring);
alert(JsonParseData); ///BREAKING BEFORE THIS LINE
几个错误。
需要将字符串放在双引号中("
)。将 "businessID": ChIJ49DlQ5NiwokRQ_noyKqlchQ
替换为 "businessID":"ChIJ49DlQ5NiwokRQ_noyKqlchQ"
删除下一行末尾的 ,
"businessname":"Wong's Garden"},]
JSON的键值需要引号。 JSON 数据中的引号少了,终于多了一个逗号和一个回车 return
这样是对的:
[{"businessID":"ChIJ49DlQ5NiwokRQ_noyKqlchQ","latitude":"40.733038","longitude":"-73.6840691","address":"1201 Jericho Turnpike, New Hyde Park","businessname":"SUBWAY®Restaurants"},{"businessID":"ChIJZfl6R5NiwokRZo7PU4NPoMY","latitude":"40.7329359","longitude":"-73.684513","address":"1113 Jericho Turnpike, New Hyde Park","businessname":"Gino's"},{"businessID":"ChIJcbpnRJNiwokRrtbOKe7HQo0","latitude":"40.733049","longitude":"-73.684006","address":"1203 Jericho Turnpike, New Hyde Park","businessname":"Wong's Garden"}]
您的回复是无效的 json 格式,原因有两个:
- "businessID" 的值需要引号。
- JSON 的最后一个对象后不应有逗号(您的替换字符串函数修复了此问题)。
我推荐你使用这个 JSON 工具包:
- http://jsonviewer.stack.hu/(这命令我的 json 虽然不正确)
- https://jsonformatter.org/(我经常用这个,我的最爱)
所以在收到我的回复后,我一直收到解析错误。有非法字符吗?
这是回复
[{"businessID": ChIJ49DlQ5NiwokRQ_noyKqlchQ,"latitude": 40.733038,"longitude":-73.6840691,"address":"1201
Jericho Turnpike, New Hyde Park","businessname":"SUBWAY®Restaurants"},{"businessID": ChIJZfl6R5NiwokRZo7PU4NPoMY
,"latitude": 40.7329359,"longitude":-73.684513,"address":"1113 Jericho Turnpike, New Hyde Park","businessname"
:"Gino's"},{"businessID": ChIJcbpnRJNiwokRrtbOKe7HQo0,"latitude": 40.733049,"longitude":-73.684006,"address"
:"1203 Jericho Turnpike, New Hyde Park","businessname":"Wong's Garden"},]
这是我处理响应的函数。我确定它在警报之前就中断了,因为警报没有被触发
var datad = $(msg).text();
console.log(datad);
var resultstring = datad.replace(',]',']');
var JsonParseData = JSON.parse(resultstring);
alert(JsonParseData); ///BREAKING BEFORE THIS LINE
几个错误。
需要将字符串放在双引号中(
"
)。将"businessID": ChIJ49DlQ5NiwokRQ_noyKqlchQ
替换为"businessID":"ChIJ49DlQ5NiwokRQ_noyKqlchQ"
删除下一行末尾的
,
"businessname":"Wong's Garden"},]
JSON的键值需要引号。 JSON 数据中的引号少了,终于多了一个逗号和一个回车 return
这样是对的:
[{"businessID":"ChIJ49DlQ5NiwokRQ_noyKqlchQ","latitude":"40.733038","longitude":"-73.6840691","address":"1201 Jericho Turnpike, New Hyde Park","businessname":"SUBWAY®Restaurants"},{"businessID":"ChIJZfl6R5NiwokRZo7PU4NPoMY","latitude":"40.7329359","longitude":"-73.684513","address":"1113 Jericho Turnpike, New Hyde Park","businessname":"Gino's"},{"businessID":"ChIJcbpnRJNiwokRrtbOKe7HQo0","latitude":"40.733049","longitude":"-73.684006","address":"1203 Jericho Turnpike, New Hyde Park","businessname":"Wong's Garden"}]
您的回复是无效的 json 格式,原因有两个:
- "businessID" 的值需要引号。
- JSON 的最后一个对象后不应有逗号(您的替换字符串函数修复了此问题)。
我推荐你使用这个 JSON 工具包:
- http://jsonviewer.stack.hu/(这命令我的 json 虽然不正确)
- https://jsonformatter.org/(我经常用这个,我的最爱)