JSON Basics - JSON basics
(Page 2 of 5 )
JSON has simple types and two structures which are very similar to the universally used data structures such as dictionary objects, hash values, key/value pairs, lists, sequences, record sets, arrays and so on. It is a natural.
Types in JSON
JSON's JavaScript subset admits of the following four primitive types.
- strings [except ", and Control Characters]
- number [Integer, real and Floating point]
- Boolean [literals, true and false]
- null
Unlike JavaScript it does not support hexadecimal and octal representation of numbers, nor does it support NaN (not a number) and infinity. Also numbers do not need quotes but strings do.
In addition to the four primitives it also supports two kinds of structures, object and array.
JSON Object
A JSON object is an unordered set (members) of key/value pairs, with keys being separated by values using a colon (:) and the members being separated by a comma (,). The colon and the comma are respectively called the name separator and the value separator.
Here is a variable that represents a JSON object with three members. "company":"Hodentek" is one member of the JSON object where the key, which is "company," is separated by the value, which is "Hodentek." The object is always enclosed between curly brackets as shown in the declared variable. In this example all the values are of type string.
var jObj={"company":"Hodentek", "phone":"609-275-1205", "city":
"Plainsboro"};
You can access any member's value using the dot notation as shown in the next snippet, which shows the "phone" of the object in the alert as "609-275-1205."
alert(jObj.phone);//
The values of members of the JSON object can be of different types as shown in the following code listing.
<SCRIPT LANGUAGE="JavaScript">
//values are numbers (integer and floating point)
var jobj2={"sunday":1, "tuesday":2.85, "wednesday":3};
document.write("<b>jobj2.tuesday:</b> " + jobj2.tuesday +"<br>");
//values are of mixed types
var jObj3={"culprit":true, "innocent":false, "notsure":null};
document.write("<b>jObj3.culprit:</b> " + jObj3.culprit +"<br>");
document.write("<b>jObj3.innocent:</b> " + jObj3.innocent +"<br>");
document.write("<b>jObj3.notsure:</b> " + jObj3.notsure +"<br>");
</SCRIPT>
The displayed output when this code is browsed is as shown in the next picture.

Next: Array >>
More JavaScript Articles
More By Jayaram Krishnaswamy