WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSON Function Files


A regular utilization of JSON is to peruse information from a web server, what's more, show the information in a web page.

This part will show you, in 4 easy advances, how to peruse JSON information, utilizing capacity files.


JSON Example

This precedent peruses a menu from myTutorials.js, and shows the menu in a web page:

JSON Example

<div id="id01"></div>

<script>
function myFunction(arr) {
    var out = "";
    var i;
    for(i = 0; i<arr.length; i++) {
        out += '<a href="' + arr[i].url + '">' + arr[i].display + '</a><br>';
    }
    document.getElementById("id01").innerHTML = out;
}
</script>

<script src="myTutorials.js"></script>

Example Explained

1: Create a variety of objects.

Use a array literal to pronounce a array of objects.

Give each item two properties: display and url.

Name the cluster myArray:

myArray

var myArray = [
{
"display": "JavaScript Tutorial",
"url": "http://www.welookups.com/js/default.asp"
},
{
"display": "HTML Tutorial",
"url": "http://www.welookups.com/html/default.asp"
},
{
"display": "CSS Tutorial",
"url": "http://www.welookups.com/css/default.asp"
}
]

2: Create a JavaScript capacity to show the array. 

Create a capacity myFunction() that circles the exhibit items, and show the substance as HTML joins:

myFunction()

work myFunction(arr) {
    var out = "";
    var i;
    for(i = 0; I < arr.length; i++) {
        out += '<a href="' + arr[i].url + '">' + arr[i].display + '</a><br>';
    }
    document.getElementById("id01").innerHTML = out;
}

Call myFunction() with myArray as argument:

Example

myFunction(myArray);

3: Use a cluster exacting as the contention (rather than the exhibit variable):

Call myFunction() with a cluster literal as argument:

Calling myFunction()

myFunction([
{
"display": "JavaScript Tutorial",
"url": "http://www.welookups.com/js/default.asp"
},
{
"display": "HTML Tutorial",
"url": "http://www.welookups.com/html/default.asp"
},
{
"display": "CSS Tutorial",
"url": "http://www.welookups.com/css/default.asp"
}
]);

4: Put the capacity bring in an outside js file

Put the capacity call this in a document named myTutorials.js:

myTutorials.js

myFunction([
{
"display": "JavaScript Tutorial",
"url": "http://www.welookups.com/js/default.asp"
},
{
"display": "HTML Tutorial",
"url": "http://www.welookups.com/html/default.asp"
},
{
"display": "CSS Tutorial",
"url": "http://www.welookups.com/css/default.asp"
}
]);

Add the outside content to your page (rather than the capacity call):

Add External Script

<script src="myTutorials.js"></script>