<!DOCTYPE html>
<html>
<head>
<script data-require="jquery@2.1.1" data-semver="1.9.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="example.js"></script>
<style>body{ background: #F9F9FA; }</style>
</head>
<body>
<a href="#" id="get-data">Get JSON data</a>
<div id="show-data"></div>
</body>
</html>
$(document).ready(function () {
$('#get-data').click(function () {
var showData = $('#show-data');
$.getJSON('example.json', function (data) {
console.log(data);
var items = data.items.map(function (item) {
return item.key + ': ' + item.value;
});
showData.empty();
if (items.length) {
var content = '<li>' + items.join('</li><li>') + '</li>';
var list = $('<ul />').html(content);
showData.append(list);
}
});
showData.text('Loading the JSON file.');
});
});
Florian Rappl demonstrates how to use jQuery's getJSON helper to load JSON-encoded data from a server using a GET HTTP request.
http://www.sitepoint.com/ajaxjquery-getjson-simple/
{
"items": [
{
"key": "First",
"value": 100
},{
"key": "Second",
"value": false
},{
"key": "Last",
"value": "Mixed"
}
],
"obj": {
"number": 1.2345e-6,
"enabled": true
},
"message": "Strings have to be in double-quotes."
}