首页 > Web开发 > 详细

JSON.parse解决Unexpected token ' in JSON at position 1报错

时间:2019-12-18 11:34:33      阅读:73      评论:0      收藏:0      [点我收藏+]

JSON.parse解决Unexpected token ‘ in JSON at position 1报错

 壹 ? 引

我们知道JSON.parse能将JSON字符串转变成JS对象,但在一些转换中可能出现Unexpected token ‘ in JSON at position 1的错误,这是因为被转换的值不符合JSON格式而造成的。

JSON官方明确规定,JSON数据的key与value必须使用双引号""包裹,否则在转换过程中会导致错误。

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

 贰 ? 示例

技术分享图片
// 数组
let a = ‘["a","b","c"]‘;// √
let b = "[‘a‘,‘b‘,‘c‘]";// X

// 对象
let a1 = ‘{"name":"听风是风","age":"26"}‘;// √
let b1 = "{‘name‘:‘听风是风‘,‘age‘:‘26‘}";// X

console.log(JSON.parse(a))// Array
console.log(JSON.parse(a1))// Object
console.log(JSON.parse(b))// 报错
console.log(JSON.parse(b1))// 报错
技术分享图片

技术分享图片

 

 

本文转自:  https://www.cnblogs.com/echolun/p/11720161.html

JSON.parse解决Unexpected token ' in JSON at position 1报错

原文:https://www.cnblogs.com/autoXingJY/p/12058694.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!