Tuesday, 1 October 2013

json details



JSON
·         JSON stands for JavaScript Object Notation. Shortly, it is a data representation format.
·         It is a lightweight data interchange format for creating objects that is a part of and built into javascript.
·         JSON provides a simple alternative to the Fatty XML format for data representation.

Example:
JSON Objects:
JSON objects are written inside curly brackets. Objects can contain multiple key/values pairs. Each key is a property of the JSON object.

var obj1 = { name : "Amit", age : 20, country : "India" };
var obj2 = { name : "John", age : 23, country : "US" };
var obj3 = { name : "Alex", age : 28, country : "France" };
var obj4 = { u1 : obj1, u2 : obj2, u3 : obj3 };

document.write(obj2.name);             //  result : John
document.write(obj4.u3. country);      //  result : France
document.write(obj4["obj1"]["Name"]);  //  result : Amit
JSON Array:

var meats = ["beef", "pork", "lamb"];
var fruits = ["apple", "orange", "grape", "plumb"];
var jsonobj = { arr1: meats, arr2: fruits };

document.write(jsonobj.arr1[0]);  //  result: beef
document.write(jsonobj.arr2[2]);  //  result: grape

Array of JSON objects:
var obj = [{ Dept: "Software", Employees: ["subrat", "ajit", "rakesh"] },
           { Dept: "Sales", Employees: ["amit", "suresh", "deepak"] },
           { Dept: "Marketing", Employees: ["sujit", "shankar", "aditya"] }];

document.write(obj[2].Dept);         //  result: Marketing
document.write(obj[1].Employees[2]); //  result: deepak

No comments:

Post a Comment