0% found this document useful (0 votes)
1 views

JSON Data Representation

JSON Data Representation

Uploaded by

alaa_ab2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

JSON Data Representation

JSON Data Representation

Uploaded by

alaa_ab2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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.
https://www.cs.virginia.edu/~up3f/cs4640/slides/4640meet13-JSON.pdf
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 = JSON.parse(data);

document.writeln("<br>firstname:"+ob.firstname);
document.writeln("<br>lastname:"+ob.lastname);

</script>

</body>
</html>

https://www.logilax.com/javascript-parse-json

You might also like