Java JSON Tutorial - JSON Data Types

Java JSON Tutorial - JSON Data Types


The following table lists the data types supported by JSON.
TypeDescription
Numberdouble-precision, floating-point format in JavaScript. Octal and hexadecimal formats are not used. No NaN or Infinity. Example, 1,9, 0,-4. Fractions like .3, .9
Exponent like e, e+, e-,E, E+, E-
var json-object-name = { string : number_value}
Stringdouble-quoted Unicode with backslash escaping.
Escape sequence: \b \f \n \r \t \u
var json-object-name = { string : "string value"}
Booleantrue or false
var json-object-name = { name: true/false,}
ArrayAn ordered sequence of values.
Array elements are enclosed square brackets [element,element,element, ].
ValueCan be a string, a number, true or false, null etc
ObjectAn unordered collection of key:value pairs.
Object are enclosed in curly braces starts with '{' and ends with '}'.
key:value pairs are separated by ,
The keys must be strings and should be different from each other.
{ string : value, string1 : value1,.......}
Whitespacecan be used between any pair of tokens
nullempty



Example

Example showing Number Datatype, value should not be quoted:
var obj = {grade: 97}
Example of String data type.
var obj = {name: 'abc'}
var obj = {name: 'Jack', 
grade: 97,
pass: true}
The following JSON data has an array of three books.
{
"books": [
{ "language":"Java" , "edition":"second" },
{ "language":"C++" , "edition":"fifth" },
{ "language":"C" , "edition":"third" }
]
}
Example showing Object:
{
"id": "1",
"language": "Java",
"page": 500,
}

Post a Comment

Previous Post Next Post