Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have trouble wrapping my head around a peculiar feature of the JSON data format.

The situation is as follows: I have a string containing a Windows (sigh) directory path, backslashes escaped. For some reason, the jQuery JSON parser thinks that a single escape is not enough.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">

var success = jQuery.parseJSON('{"a":"b:\\c"}');
var failure = jQuery.parseJSON('{"a":"b:\c"}');

</script>

Can anyone explain what makes such double escaping necessary?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
305 views
Welcome To Ask or Share your Answers For Others

1 Answer

The first escape escapes it in the Javascript string literal.
The second escape escapes it in the JSON string literal.

The Javascript expression '{"a":"b:\c"}' evaluates to the string '{"a":"b:c"}'.
This string contains a single unescaped , which must be escaped for JSON. In order to get a string containing \, each must be escaped in the Javascript expression, resulting in "".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...