JSON Encoding
by bryandowning
JavaScript
// Which of the following JSON strings will fail when you parse them using JSON.parse?
var JSONStrings = [];
var xhr, data;
// 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"}]');
data = { json: '[{"name": "Dwayne Johnson"}]', delay: 3 };
$.post('/echo/json/', data, function(){
console.log(arguments);
});
for( var i=0, len=JSONStrings.length; i<len; i++ ){
break;
var str = JSONStrings[i];
try {
console.log( JSON.parse(str) );
} catch( e ) {
console.log( i, e );
}
}