JSON is getting popular by the days. JSON stands for JavaScript
Object Notation. It is used in data representation as well as data
transmission.
To very if a string is a valid JSON string, use the JSONLint at http://jsonlint.com
JSON Data Types
JSON supports the following data types:
- Object
- String
- Boolean
- Number
- Array
- null
The following sections will elaborate on each of the data
types.
An Object is an
unordered collection of key:value pairs enclosed in a pair of curly braces ({}). The following is an example of an
empty object:
{}
String
The key in an object must be a String, while the value can be either a String, Boolean, Number, Array,
null, or another Object.
The following shows an object with one key:value pair:
{
"firstName": "John"
}
An object can have multiple key:value pairs, for example:
{
"firstName":
"John",
"lastName": "Doe"
}
Note the comma (,) after John and there is no comma after
Doe.
Each key in the Object
must be unique. For example, the following example is not a valid JSON string:
{
"firstName":
"John",
"firstName": "Doe"
}
Boolean
A Boolean value
can either be true or false:
{
"firstName":
"John",
"lastName":
"Doe",
"isMember": true
}
Number
A Number value
can either be an integer, or a floating-point number:
{
"firstName":
"John",
"lastName":
"Doe",
"isMember":
true,
"weight": 79.5,
"height": 1.73,
"children": 3
}
Nested Object
The value of a key can also be another Object, as the following example shows:
{
"firstName":
"John",
"lastName":
"Doe",
"isMember":
true,
"weight":
79.5,
"height":
1.73,
"children":
3,
"address": {
"line1": "123
Street",
"line2": "San
Francisco",
"state": "CA",
"postal": "12345"
}
}
Array
An Array is an
ordered sequence of Objects:
{
"firstName":
"John",
"lastName":
"Doe",
"isMember":
true,
"weight":
79.5,
"height":
1.73,
"children":
3,
"address": {
"line1":
"123 Street",
"line2":
"San Francisco",
"state":
"CA",
"postal":
"12345"
},
"phone": [
{
"type": "work",
"number":
"1234567"
},
{
"type": "home",
"number":
"8765432"
},
{
"type":
"mobile",
"number":
"1234876"
}
]
}
Note that arrays are denoted with a pair of brackets ([]).
null
When a key has no value, you can assign a null to it:
{
"firstName":
"John",
"lastName":
"Doe",
"isMember":
true,
"weight":
79.5,
"height":
1.73,
"children":
3,
"address": {
"line1":
"123 Street",
"line2":
"San Francisco",
"state":
"CA",
"postal":
"12345"
},
"phone": [
{
"type": "work",
"number": "1234567"
},
{
"type": "home",
"number": "8765432"
},
{
"type": "mobile",
"number": "1234876"
}
],
"oldMembershipNo": null
}
No comments:
Post a Comment