L09-JSON
L09-JSON
JSON
Joseph Tonien
JavaScript Object Notation (JSON)
{
"firstName":"John",
"lastName":"Smith",
"domestic":true,
"fee":100.5
}
JSON
{
"firstName":"John",
"lastName":"Smith",
"domestic":true,
"fee":100.5
}
JSON
objJSON = JSON.stringify(obj);
obj = JSON.parse(objJSON);
JSON
Example 1: http://www.uow.edu.au/~dong/w3/example/json/example1.html
function showJSON(){
//create a student object
var john = {};
john.firstName = "John";
john.lastName = "Smith";
john.domestic = true;
john.fee = 100.50;
<button onClick="showJSON()">
Click here to see JSON string
</button>
JSON
Example 2: http://www.uow.edu.au/~dong/w3/example/json/example2.html
function showObject(){
//JSON string
var johnJSON = '{"firstName":"John","lastName":"Smith",
"domestic":true,"fee":100.5}';
<button onClick="showObject()">
Click here to see object from JSON
</button>
JSON
[
{
"firstName":"John",
"lastName":"Smith"
},
{
"firstName":"Kate",
"lastName":"Williams"
}
]
JSON
Example 3: http://www.uow.edu.au/~dong/w3/example/json/example3.html
function showJSON(){
var john = {};
john.firstName = "John";
john.lastName = "Smith";
function showArray(){
//JSON string
var studentListJSON = '[{"firstName":"John","lastName":"Smith"},
{"firstName":"Kate","lastName":"Williams"}]';
<button onClick="showArray()">
Click here to see array from JSON
</button>
JSON
Example 5: http://www.uow.edu.au/~dong/w3/example/json/example5.html
function showJSON(){
var john = {}; //create a student object
john.firstName = "John";
john.lastName = "Smith";
john.enrolledSubjects = [];
//empty array to hold subjects
{
"firstName":"John",
"lastName":"Smith",
"enrolledSubjects":[
{
"code":"MATH101",
"title":"Algebra"
},
{
"code":"CSIT122",
"title":"C programming"
}
]
}
Ajax-JSON example: Stock market
Assume that there is a JSON file, called market.json. Write HTML and
JavaScript codes that do the following:
There is a button “Click here to view Stock Market Activity”. When the user clicks on
this button, make an Ajax call to get the stock information from the JSON file and
display them in a table. http://www.uow.edu.au/~dong/w3/example/json/stock/market.html
Ajax-JSON example: Stock market
This is the content of the JSON file market.json
{
"queryTime":"24/02/2015 11:30:00",
"indexList":[
{
"name":"NASDAQ",
"value":4725.64,
"change":-37.58,
"netPercentage":0.79
},
{
"name":"NASDAQ-100 (NDX)",
"value":4312.01,
"change":-29.38,
"netPercentage":0.68
},...
]
}
Ajax-JSON example: Stock market
<button onClick="getMarketAjax()">
Click here to view Stock Market Activity
</button>
function getMarketAjax(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
processResult(xhttp);
}
};
xhttp.open("GET", "market.json", true);
xhttp.send();
}
{
Ajax-JSON example: Stock market "queryTime":"24/02/2015 11:30:00",
"indexList":[
function processResult(xhttp){ {
1 "name":"NASDAQ",
var jsonText = xhttp.responseText; "value":4725.64,
"change":-37.58,
var marketObj = JSON.parse(jsonText);
"netPercentage":0.79
display(marketObj); },...
]
} }
3
{
Ajax-JSON example: Stock market "queryTime":"24/02/2015 11:30:00",
"indexList":[
function processResult(xhttp){ {
1 "name":"NASDAQ",
var jsonText = xhttp.responseText; "value":4725.64,
"change":-37.58,
var marketObj = JSON.parse(jsonText);
"netPercentage":0.79
display(marketObj); },...
]
} }
function processResult(xhttp){ {
1 "name":"NASDAQ",
var jsonText = xhttp.responseText; "value":4725.64,
http://www.uow.edu.au/~dong/w3/example/json/stock/market.html
and
http://www.uow.edu.au/~dong/w3/example/json/stock/market2.html
References
http://www.w3schools.com/json