最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $.parseJSON breaking with double quotes - Stack Overflow

programmeradmin3浏览0评论

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":""""}}]

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":""""}}]
Share Improve this question edited Nov 20, 2011 at 2:01 Clive 37k8 gold badges89 silver badges113 bronze badges asked Nov 19, 2011 at 17:22 Tomas ReimersTomas Reimers 3,2924 gold badges25 silver badges37 bronze badges 5
  • Please post an example of the JSON that is breaking the parser. – Scott A Commented Nov 19, 2011 at 17:24
  • We need more information to give a meaningful answer. Nobody here can mind-read. You may want to look into the JSON specification (json) though; JSON itself is fairly straightforward. – tdammers Commented Nov 19, 2011 at 17:25
  • Can you provide an example please? – Shaun Commented Nov 19, 2011 at 17:25
  • Sorry to all, I accidentally pressed enter when making my question (whoops) and hurried to make a semi acceptable one so I could edit it to the full question. my bad – Tomas Reimers Commented Nov 19, 2011 at 17:27
  • I had the same issue validation shows NO error so i think its problem with jquery 1.7. For me solution whose remove switch: JSON_UNESCAPED_UNICODE in PHP json_encode( – user956584 Commented Aug 13, 2012 at 19:47
Add a ment  | 

5 Answers 5

Reset to default 9

You have an extra } at the end.

}}]

You should run troublesome JSON markup through http://jsonlint./

Parse error on line 6:
..."""    }}]
---------------------^
Expecting ',', ']'

It's not the " or " but the extraenous } you have:

[{"user":"tomasa", "time":"1321723278", "text":""""}}]
                                                              ^

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=""""}]

$.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?

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.

发布评论

评论列表(0)

  1. 暂无评论