Call (877) SITE-OPT (748-3678)

Code: JSON data format

Chapter 8 - Ajax Optimization

As an alternative to XML, many Ajax developers prefer to use JSON, available at http://www.json.org. JSON is a lightweight data-interchange format that is based on a subset of the JavaScript language. Because it is a subset of JavaScript, we can very easily convert a JSON response into something that the receiving script can consume directly.

For example, we may return the following, which is a valid JSON array:

["value1","value2","value3","value4"]

Or we might return a JSON object literal such as this:

{"today": "Wednesday", "planet" : "Earth" , "mood" : "happy" }

If this were found in the responseText property of an XHR object, we might quickly make it available for use via this:

var result = eval(xhr.responseText);

And now you would be able to reference the individual values in the result object, like so:

var str = "Today is " + result.today + " and on planet "+ result.planet + " people
are generally " + result.mood;
Copyright © 2002-2017 Website Optimization, LLC. All Rights Reserved - Free website speed test - Privacy Policy
Last modified: February 19, 2009.

Follow us on: Twitter, Google+, Facebook, Linked In