jQuery: AJAX and JSON


In this post, we will learn jQuery AJAX using JSON data. The term JavaScript Object Notation (JSON) was coined by Douglas Crockford to capitalize on the simple syntax. JavaScript Object are just sets of key-value pairs, and can be defined succinctly using curly braces ({}). JavaScript arrays are defined on the fly with square brackets([]). Combining these two concepts will express some very complex and rich data structures.

Following Simple example JSON. This notation can offer a concise alternative to the sometimes-bulky XML format:

{
  "key": "value",
  "key 2": [
    "array item 1",
    "array item 2",
    "array item 3"
   ]
}

For more complex data, see code below:

[
{
	"company" : "Toyota",
	"product" : "Prius",
	"color" : [
	  "white pearl",
	  "Red Methalic",
	  "Silver Methalic"
	],
	"type" : "Gen-3"
},
{
	"company" : "Toyota",
	"product" : "New Fortuner",
	"color" : [
	  "Super White",
	  "Silver",
	  "Black"
	],
	"type" : "Fortuner TRD Sportivo Limited Edition"
}
]

Save above code. Named "data.json". We will use it to our jQuery AJAX using JSON in this post.

To call data.json and access data. You can use following code:

$(document).ready(function() {
  $('#element').click(function() {
    $.getJSON('data.json');
    return false;
  });
});

For more complex data, such as above data, we can use:

			$.getJSON('data.json',function(data){
				$('#content').empty();
				$.each(data, function(entryIndex, entry){
					var html = '<div class="data">';					  
					html += '<h3>' + entry['company'] + '</h3>';
					html += '<div class="product">' + entry['product'] + '</class>';					
					html += '<div class="type">' + entry['type'] + '</color>';										
					$('#content').append(html);
				});						   
			});

To be more clear, try this code. Save as "page.html". Place along with "data.json".

<html>
<head>
	<style type="text/css">
		
	</style>
	<script src="jquery.js" type="text/javascript"></script>
	<script type="text/javascript">
	$(document).ready(function() {
		$('#car').click(function() {
			$.getJSON('data.json',function(data){
				$('#content').empty();
				$.each(data, function(entryIndex, entry){
					var html = '<div class="data">';					  
					html += '<h3>' + entry['company'] + '</h3>';
					html += '<div class="product">' + entry['product'] + '</div>';					
					html += '<div class="type">' + entry['type'] + '</div>';										
					$('#content').append(html);
				});						   
			});
			return false;
		});
	});
	</script>
</head>
<body>

<a href="#" id="car">Car</a>
<div id="content">
</div>
</body>
</html>
jQuery Ajax JSON Tutorial

To handle the entries with colors, which takes another $.each() loop

	$(document).ready(function() {
		$('#car').click(function() {
			$.getJSON('data.json',function(data){
				$('#content').empty();
				$.each(data, function(entryIndex, entry){
					var html = '<div class="data">';					  
					html += '<h3>' + entry['company'] + '</h3>';
					html += '<div class="product">' + entry['product'] + '</div>';					
					html += '<div class="type">' + entry['type'] + '</div>';
					if(entry['color']){
						html += '<div class="color">';											
						html += '<ol>';
						$.each(entry['color'],function(colorIndex, color){
							html += '<li>' + color + '</li>';						   
						});	
						html += '</ol>';						
						html += '</div>';												
					}
					$('#content').append(html);
				});						   
			});
			return false;
		});
	});
jQuery AJAX JSON Tutorial Complex data



Bookmark and Share Tag: jquery, JavaScript, JavaScript Framework Category: JavaScript Post : September 09th 2010 Read: 74,178

advertisements


blog comments powered by Disqus