Data representation:
JSON vs. XML
JSON uses key-value pairs, while XML represents data in a tree pattern.
The following examples display the same information in both data
representations.
Example: JSON document
The following example displays the names of three guests in JSON.
{"guests":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"María", "lastName":"García" },
{ "firstName":"Nikki", "lastName":"Wolf" }
]}
How does JSON work?
• Data are presented in property name-value pairs
• Strings and property names (or keys) must be placed in double
quotes
• The key is separated from its value by a colon
• Each key-value pair is separated by a comma. No comma after the
last key-value pair.
[Link]
Example: XML document
The following example displays the names of three guests in XML.
<guests>
<guest>
<firstName>John</firstName> <lastName>Doe</lastName>
</guest>
<guest>
<firstName>María</firstName> <lastName>García</lastName>
</guest>
<guest>
<firstName>Nikki</firstName> <lastName>Wolf</lastName>
</guest>
</guests>
How to Parse JSON in JavaScript: important
• A common use of JSON is to read data from a web server, and display the data in a web page.
<!DOCTYPE html>
<html>
<head>
<title>JSON Parsing</title>
</head>
<body>
<script type="text/javascript">
var data = '{"firstname":"Ala","lastname":"Abuthawabeh","university":"AAU"}';
var ob = [Link](data);
[Link]("<br>firstname:"+[Link]);
[Link]("<br>lastname:"+[Link]);
</script>
</body>
</html>
[Link]