5 April 2014

JSON Datatypes



JSON supports following datatypes:

  1. Number
  2. String
  3. Boolean
  4. Array
  5. Object
  6. null


Number
It can be a decimal integer or decimal floating point number. Octal and hexadecimal formats are not supported.
Syntax
var json_num_obj = { “string” : number_value1, string : number_value2…….}
Usage
var json_student= { “maths”: 80, “science” : 87, “social” :78}

String
It is a sequence of zero or more characters put inside as double quotes.
Syntax
var json_str_obj = { “string” : “str_value1″, “string” : “str_value2″…….}
Usage
var json_str_fullname = { “firstName”:”Programmer” , “lastName”:”Guru” }

Boolean
It includes true or false values.
Syntax
var json_bool_obj = { “string”: true/false…….}
Usage
var json_stud_rank = { “distinction”:true , “class topper”:true }

Array
It is an ordered collection of values.
The values are enclosed square brackets which means that array begins with ‘[' and ends with ']‘
The values are separated by ,(comma).
Array index starts from 0.
Syntax
var json_bool_obj = { “string1″: [
{"string2": "string":"value"},
{"string2": "string":"value"},
{"string2": "string":"value"},
]
}
Usage
var json_lang_array =
{ “languages”: [
{ "language":"C" , "Creator":"Dennis Ritchie" },
{ "language":"C++" , "Creator":"Bjarne Stroustrup" },
{ "language":"Java" , "Creator":"James Gosling" }
]
}

Object
It is an unordered set of name/value pairs.
Object are enclosed in curly braces like it starts with ‘{‘ and ends with ‘}’.
Each name is followed by ‘:’(colon) and the name/value pairs are separated by , (comma). Make sure both name and value are enclosed within double quotes.
The keys must be strings and should be different from each other.
Syntax
var json_bool_obj = { “string1″: “value”.”string2″:”value”……..}
Usage
var json_obj = { “Language”:”JSON” , “Author”:”Douglas Crockford”}

Null
It means empty type.
Syntax
var json_null_obj = { “Language”:null }
Usage
var i = null;
if(i==1)
{
document.write(“<h1>value is 1</h1>”);
}
else
{
document.write(“<h1>value is null</h1>”);
}

No comments:

Post a Comment