Jquery Knowledgebase: A simple JSON example
1) External JSON file
On the server in the same directory as your html file in part 2), named as "samplejson.js" :
{
"employees": [
{ "firstName":"John" , "lastName":"Jones" },
{ "firstName":"Matthew" , "lastName":"Carlsson" },
{ "firstName":"Erik" , "lastName":"Bolt" }
]
}
2) In the HTML or .Javascript files
Within the Javascript (assuming that Jquery is present also the document is ready and there is a HTML tag with id="exampleid")
function loadJson(){
$.getJSON("samplejson.js").done(function (json) {
$("#exampleid").html(json.employees[0].lastName);
});
}
3) End result
The HTML element with id="exampleid" will change to "Jones" when the function loadJson is run. |