JSON.parse解决UnexpectedtokeninJSONatposition1报错
error parse new壹❀引
我们知道JSON.parse能将JSON字符串转变成JS对象,但在⼀些转换中可能出现Unexpected token ' in JSON at position 1的错误,这是因为被转换的值不符合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))// 报错
若你对JSON.stringify()与JSON.parse()区别有所疑惑,以及它们在实际开发中有哪些作⽤,欢迎阅读博主这篇⽂章。
希望对你有所帮助,那么本⽂到此结束。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。