CS 80: Internet Programming
Instructor: Mark Edmonds
<!-- XML to represent a list of employees-->
<employees>
<employee>
<firstName>John</firstName>
<lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName>
<lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName>
<lastName>Jones</lastName>
</employee>
</employees>
{"attribute" : "value" , ...
}
model for representing data
[]
can be the value, indicating an
array
// json to represent a list of employees
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
JSON.parse(json_text)
where
json_text
is valid JSON text
(e.g. the text above)simple_json.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple JSON example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
// run when the document is ready
$(document).ready(function(){
// create JSON data
var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}';
// parse it into an object
var obj = JSON.parse(text);
// access members/attributes of the new object
document.getElementById("demo").innerHTML =
obj.name + "<br>" +
obj.street + "<br>" +
obj.phone;
});
</script>
</head>
<body>
<h2>JSON Object Creation in JavaScript</h2>
<p id="demo"></p>
</body>
</html>
employees
JSON object above?employee_json.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Employee JSON example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
// run when the document is ready
$(document).ready(function() {
// create JSON text
var data = '{"employees": [ {"firstName": "John","lastName": "Doe" }, {"firstName": "Anna", "lastName": "Smith"}, {"firstName": "Peter","lastName": "Jones" }]}';
// parse JSON text into object
var employees_obj = JSON.parse(data);
// setup document
document.writeln("<h2>Employees list</h2>");
document.writeln("<p>");
// loop over every employee
for (var i = 0; i < employees_obj.employees.length; i++) {
// print employee info
document.writeln(employees_obj.employees[i].firstName + " " + employees_obj.employees[i].lastName + "<br>");
}
document.writeln("</p>");
});
</script>
</head>
<body>
</body>
</html>
employee_js.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Employee JSON example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
// run when the document is ready
$(document).ready(function() {
// create JS object using JSON syntax
var employees = [{ "firstName": "John","lastName": "Doe" }, {"firstName": "Anna", "lastName": "Smith"}, {"firstName": "Peter","lastName": "Jones" }];
// setup document
document.writeln("<h2>Employees list</h2>");
document.writeln("<p>");
// loop over every employee
for (var i = 0; i < employees.length; i++) {
// print employee info
document.writeln(employees[i].firstName + " " + employees[i].lastName + "<br>");
}
document.writeln("</p>");
});
</script>
</head>
<body>
</body>
</html>