Reputation: 3292
Can someone pleas explain to me why json which contains a string with double quotes will break $.parseJSON
?
This works:
[{"type":"message","content":{"user":"tomasa", "time":"1321722536", "text":"asdasdasd"}}]
This also works:
[{"type":"message","content":{"user":"tomasa", "time":"1321723267", "text":"""}}]
However this will cause $.jsonParse
to not return anything (I am assuming becuase it is a malformed json string:
[{"user":"tomasa", "time":"1321723278", "text":""""}}]
Upvotes: 0
Views: 4437
Reputation: 606
It crashes because of the double }
.
>>> $.parseJSON('[{"user":"tomasa", "time":"1321723278", "text":""""}}]')
SyntaxError: JSON.parse: expected ',' or ']' after array element
(function(a,b){function cy(a){return f...h]&&f.event.trigger(c,d,b.handle.elem
But this works:
>>> $.parseJSON('[{"user":"tomasa", "time":"1321723278", "text":""""}]')
[Object { user="tomasa", time="1321723278", text=""""}]
Upvotes: 3
Reputation: 1678
Because the JSON specification specifically states that string
elements are defined as:
string
""
" chars "
In other words, string values must be surrounded by double quotes in order to be valid JSON.
Edited to add:
My answer above is correct for the original question as it was posted a few seconds ago, which was basically "why does jQuery.parseJSON fail when double quotes aren't used", but then the OP modified the question to include an example that demonstrates his/her actually problem, which has nothing to do with the quotes at all.
Upvotes: 0
Reputation: 154968
It's not the "
or "
but the extraenous }
you have:
[{"user":"tomasa", "time":"1321723278", "text":""""}}]
^
Upvotes: 5
Reputation: 11327
You have an extra }
at the end.
}}]
You should run troublesome JSON markup through http://jsonlint.com/
Parse error on line 6:
...""" }}]
---------------------^
Expecting ',', ']'
Upvotes: 9
Reputation: 1853
$.parseJSON is looking for the name of the object in double quotes and the value of that object in single quotes. Is this what you are asking?
Upvotes: 0