Wednesday, July 30, 2014

JSON

JSON Stands for JavaScript Object Notation. It can be interpreted as the syntax/naming convention of defining objects in javascripts.
Data is stored in the form of key-value pairs.
A value can be an object as well (which is again a JSON). It supports nested objects
Key can be set with or without a string wrapper.

var JSONObject1 = {”key” : 1, “Key2” : 2}

var  JSONObject2= {key1: 1, key2 :2};

Symbol
Used for
{}
Wraps the object
[]
Wraps an array
“” or
Wraps a string
:
Key value separator or assignment operator
,
Element separator on arrays or objects
Example:
-          var JSONObject = {“key” : value, “key” : valye};
-          var nestedJSONObject = {“key” : value, “key” : {“key” : value}};
-          var JSONObjectWith Array = {“key” : value, “key” : [value1,value2, value3]};


Accessing properties of a JSON

 ·         Properties in a JSON object can be accessed using “.”

var myJSONObject= {“bindings” : [
{“ircEvent” : “PRIVMSG”, “method” : “newURI”, “regex” : “^http://.*”}
{“ircEvent” : “PRIVMSG”, “method” : “deleteURI”, “regex” : “^delete.*”}
{“ircEvent” : “PRIVMSG”, “method” : “randomURI”, “regex” : “^random.*”}

]
};
console.log(myJSONObject.bindings[0].method); /*newURI */

 ·         They can also be accessed in the same way that we access the values inside an array

var myJSONObject= {“bindings” : [
{“ircEvent” : “PRIVMSG”, “method” : “newURI”, “regex” : “^http://.*”}
{“ircEvent” : “PRIVMSG”, “method” : “deleteURI”, “regex” : “^delete.*”}
{“ircEvent” : “PRIVMSG”, “method” : “randomURI”, “regex” : “^random.*”}

]
};
console.log(myJSONObject.bindings[0].method); /*newURI */

No comments:

Post a Comment