I have a string that contains an array of object '[{letter: a, number: 1}, {letter: b, number: 2}]'
I'm trying to make it literally a array of object using JSON.parse()
but I'm getting an error saying
`SyntaxError: Unexpected token l in JSON at position 2`
My code
let a = '[{letter: \'a\', number: 1}, {letter: \'b\', number: 2}]'
let b = JSON.parse(a)
console.log(b)
I can't even use a.split(',')
because I have a ,
inside the object
my expected output was
[{letter: 'a', number: 1}, {letter: 'b', number: 2}]
how can I fix it?
In JSON keys must be strings, so you have to reformat your JSON string like this:
[{"letter": "a", "number": 1}, {"letter": "b", number: 2}]
More info, see this article
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.