C
C
cajka-d2019-07-17 12:15:01
JavaScript
cajka-d, 2019-07-17 12:15:01

Why is jQuery.parseJSON throwing an error?

Hello.
I use this code

var j = '["foo": 1]';
var object = jQuery.parseJSON(j);


Nothing special, the json string is written correctly, the script itself is utf-8 encoded. And it gives the error "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data".
Browser Firefox Developer Edition

Tell me, what could be the matter?

with curly '{"foo":1}' also tried, the same error.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hzzzzl, 2019-07-17
@cajka-d

objects with curly braces

j = '{"foo": 1}'
JSON.parse(j)

R
Rsa97, 2019-07-17
@Rsa97

In JSON, square brackets represent an array, and you're trying to fit an object into them.
'{"foo":1}'or '[1]'or '[{"foo":1}]', depending on what you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question