Video tutorial illustrates the extraction of JSON Array elements using jQuery AJAX method: getJSON
Here we extract data(JSON ARRAY) from an external JSON file, which has a file extension .json
HTML code
index.html
Fetch JSON array Data
Here we have a unordered list. We’ll be filling it’s list items from the JSON file using jQuery.
JSON File
json_data.json
{
"person": [
{ "name": "Satish", "age": 25 },
{ "name": "Shwetha", "age": 24 }
]
}
Here we have a key called “person”, which is a JSON array.
“person” has two elements in it, which are objects.
Each object has two key => value pairs.
The key being, name and age.
jQuery Code
my_script.js
$(document).ready( function() {
$.getJSON("json_data.json", function(data){
$.each(data.person, function(){
$("ul").append("
Once the document loads, we call an anonymous function. Inside that, we call the shortcut function of jQuery AJAX method i.e., $.getJSON method.
The first parameter is the file to be parsed, second parameter is the callback function.
Inside the callback function, we loop through the array elements and fetch the values using its key names, and finally append it to the unordered list of index.html
Fetch JSON Array Elements Using jQuery AJAX Method: getJSON
To make the AJAX effect, you could write the jQuery coding inside a custom function and then call it for setTimeout() function etc:
setTimeout(customFunction, 4000);
setInterval(repeatFunctionCall, 4000);
slideDown().delay(4000).slideUp(); ( Chain method )
View Comments
Thanks that was so clear and simple .