JSON(JavaScript Object Notation) is a light-weight data-interchange format. It is easy for humans to read and write. It is a text format that is completely language independent but uses conventions that are familiar to the programmers of the languages including C,C++,Java,JavaScript,Python and many others. This all properties make JSON an data-interchange format. It can't be constructed or called.
JSON can be represented in two different structures :
JSON can be represented in two different structures :
- A collection of name/value pairs.
- An ordered list of values.
In JSON, there are different forms. They are :
object :
An object is the set of unordered set of name/value pairs. An object begins and ends with the braces {}. Each name is followed by : (colon) and the name/value pairs are separated by comma (,).
object : { String : value }
Example :
var mainJson = {
"person" : {
"name" : "sita",
"age" : 20,
"gender" : "female",
"place" : "Hyderabad"
},
"bicycle" : {
"color" : "red",
"price" : "10000"
}
}
This is a sample example of JSON. The json data is defined in a variable called mainJson. These can be retrieved as :
To get the entire person details.
console.log(mainJson.person) // this returns the output in object form i.e,
{
"name" : "sita",
"age" : 20,
"gender" : "female",
"place" : "Hyderabad"
}
To retrieve only name : sita
console.log(mainJson.person.name) // sita
String :
To return a JSON String :
A string is a sequence of zero or more Unicode characters, wrapped in double quotes and using backslash escapes.
console.log(JSON.stringify(mainJson.person)) // "{
"name":"sita",
"age":20,
"gender":"female",
"place":"Hyderabad"
}"
object
{}
{ members }
members
pair
pair , members
pair
string : value
array
[]
[ elements ]
elements
value
value : elements
value
string
number
object
array
null
true
false
No comments:
Post a Comment