HTML and CSS Reference
In-Depth Information
FIGURE 11-12 The text visualizer to copy the JSON text to the clipboard.
With the JSON copied to the clipboard, you can go to the JSONLint website mentioned earlier,
paste the text in the editor, and validate it.
Fixing invalid JSON
It turns out that the Flickr web service doesn't always return valid JSON that can be successfully
validated against the JSONLint validator. At the same time, any JSON that JSONLint validates is
correctly parsed back by the JSON.parse function in Windows 8.
One issue you'll discover is that the Flickr service doesn't always handle the single quote character
found in some descriptions correctly. For example, consider the following text:
Wimbledon it ain\'t
This text appears in the description of some photos. To make it pass the JSON validation, you need
to double escape the single quote, so the string looks like this:
Wimbledon it ain\\'t
You can fix this problem quickly and effectively by introducing yet another extension to the String
prototype. Open the helpers.js file again and add this code:
String.prototype.doubleEscapeSingleQuotes = function () {
var theString = this;
if (theString != null && theString != "") {
return theString.replace(/\'/g, "\\'");
} else {
return theString;
}
}
Search WWH ::




Custom Search