JSON Encoding
by bryandowning
JavaScript
// Which of the following JSON strings will fail when you parse them using JSON.parse?
var JSONStrings = [];
// 0
JSONStrings.push('[{"name": "Dwayne"}]');
// 1
JSONStrings.push('[{"name": "Dwayne \"The Rock\" Johnson"}]');
// 2
JSONStrings.push('[{"name": "Dwayne \\\"The Rock\\\" Johnson"}]');
// 3
JSONStrings.push('[{"name": "Dwayne "The Rock" Johnson"}]');
for( var i=0, len=JSONStrings.length; i<len; i++ ){
var str = JSONStrings[i];
try {
console.log( JSON.parse(str) );
} catch( e ) {
console.log( i, e );
}
}