JSONLint says the following is invalid JSON:
[{
"value": 71,
"label": "123 Foobar \'eha-Kauai, Hawaii, United States"
},
{
"value": 75,
"label": "456 Foobar \'elima-Kauai, Hawaii, United States"
}]
Yet this works:
var foo = [{
"value": 71,
"label": "123 Foobar \'eha-Kauai, Hawaii, United States"
},
{
"value": 75,
"label": "456 Foobar \'elima-Kauai, Hawaii, United States"
}];
console.log(foo.length); // returns 2
Note: the string is generated by a function which could be used in the following ways:
<script>
var foo = '<%= vbs_JSONEncode("Hello World") %>'; // being inconsistent
var bar = "<%= vbs_JSONEncode(str_Hello_Msg) %>"; // with the quotes
</script>
Or simply:
Response.ContentType = "application/json"
Response.Write vbs_JSONEncode("Hello World")
JSONLint says the following is invalid JSON:
[{
"value": 71,
"label": "123 Foobar \'eha-Kauai, Hawaii, United States"
},
{
"value": 75,
"label": "456 Foobar \'elima-Kauai, Hawaii, United States"
}]
Yet this works:
var foo = [{
"value": 71,
"label": "123 Foobar \'eha-Kauai, Hawaii, United States"
},
{
"value": 75,
"label": "456 Foobar \'elima-Kauai, Hawaii, United States"
}];
console.log(foo.length); // returns 2
Note: the string is generated by a function which could be used in the following ways:
<script>
var foo = '<%= vbs_JSONEncode("Hello World") %>'; // being inconsistent
var bar = "<%= vbs_JSONEncode(str_Hello_Msg) %>"; // with the quotes
</script>
Or simply:
Response.ContentType = "application/json"
Response.Write vbs_JSONEncode("Hello World")
Share
Improve this question
edited Feb 20, 2015 at 7:20
Salman Arshad
asked May 23, 2011 at 11:34
Salman ArshadSalman Arshad
272k84 gold badges442 silver badges534 bronze badges
1
- 2 JSON is not JavaScript. Invalid JSON is not automatically invalid JavaScript. – Felix Kling Commented May 23, 2011 at 12:02
5 Answers
Reset to default 10You don't need to escape a '
with a backslash. Just escape the "
.
The issue is the escaping of the single-quote marks, which is unnecessary in JSON, since only double quotes are used.
Escape the backslashes?
Found with http://json.parser.online.fr/
There is no need for the ' to be escaped since double quotes are used. That needs to be removed and the JSON bees valid.
It looks to me like JSONLint just doesn't care too much for \
inside your JSON. Wether or not it is an invalid character for JSON, i don't know.. But as @dogbane says, no need to escape '
unless you use that char for your string encapsulation..