JSON stands for JavaScript Object Notation which is a format for storing data.
JSON is used when we have to send data from server to webpage.
JSON Example
{
"employees":[
{"firstName":"Pranav", "lastName":"Chhabra"},
{"firstName":"Mukesh", "lastName":"Tomar"},
{"firstName":"Sachin", "lastName":"Joshi"}
]
}
JSON data is written as name/value pairs
A name/value pair is made up of a field name in double quotes, followed by a colon, followed by a value:
"firstName":"Pranav"
Example
<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<p id="demo"></p>
<script>
var text = '{ "employees":[
{"firstName":"Pranav", "lastName":"Chhabra"},
{"firstName":"Mukesh", "lastName":"Tomar"},
{"firstName":"Sachin", "lastName":"Joshi"}
]}
obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
</script>
</body>
</html>
OUTPUT
Create Object from JSON String
Mukesh Tomar
0 Comment(s)