Oct 13, 2009

JSON

JSON is a popular format for sending data from the server. JSON stands for Javascript Object Notation. It's a format similar to XML, but the data is stored as Javascript. Therefore, it's easier for a javascript program to work with.

When a web server responds to an Ajax request, it can return a string formatted like JSON. The string received is not actual javascript, but text that has been formatted as a JSON object. jQuery provides a special function get(JSON) which does all the heavy lifting of conversion. The structure of the get(JSON) fuction is:

$.getJSON(url, data, callback);

The getJSON() function works the same as a post() or get() function but the data passed to the callback is a JSON object. For example if we used the following code:

$.getJSON('contact.php ',' 'contact=123', processContacts);

The contacts.php file uses information to locate a single contact in a database and retrieve that contact's information. The callback function would be:

function processContacts(data){
}

The processContacts() function has one argument data that contains the JSON object from the server. We will eventually cover this subject in class. I'm just looking ahead.
(http://www.hunlock.com/blogs/Mastering_JSON_%28_JavaScript_Object_Notation_%29)

3 comments:

  1. Jassin,

    Thanks for the informative post on cloud computing, and showing us both the pro's and con's of cloud computing. It does seem to be gaining ground, but i can't many large businesse's willing to risk their private data security or access to it just yet. It is definately helpfully for open research projects and the combined power that it has to offer tho, so i can definately see it as something that will stay with us for awhile and not just be a passing fad.

    ReplyDelete
  2. I'm not familiar with JSON but your post describing it and how it works really helps me to understand it better. Thanks for the information. When we cover this in class I hope that I can make this work.

    ReplyDelete
  3. That's a really good overview of JSON. It makes sense and I hope it is as easy to work with as you make it sound.

    ReplyDelete