*****************************************
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language. JSON is a text format that is completely language independent.
JSON is built on two structures:
* A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
* An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
1) An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
2) An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
3) A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
4) A string is a collection of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string.
5) A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
**===========================** Example of JSON Object
var myJSONObject = {"employee": [
{"name": "saju", "age": "24", "place": "kollam"},
{"name": "manu", "age": "26", "place": "kottam"},
{"name": "praveen", "age": "28", "place": "trivandrum"}
]
};
In this example, an object is created containing a single member "employee", which contains an array containing three objects bindings[0],bindings[1], and bindings[2], each containing "name", "age", and "place" members.
Members can be retrieved using dot or subscript operators.
myJSONObject.bindings[0].place // "kollam"
{} ---> object
[] ---> Array
member:value ---> Key:value
**===========================**Convert a JSON text into an object
To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure.
var myObject = eval('(' + myJSONtext + ')');
However, it can compile and execute any JavaScript program, so there can be security issues.So It is much safer to use a JSON parser.
A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval. It is expected that native JSON support will be included in the next ECMAScript standard.
var myObject = JSON.parse(myJSONtext, reviver);
The optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects(int,float,date,string) into instances of pseudoclasses, or to transform Date strings into Date objects.
**===========================**Example of JSON parser
* JSON parser Converting JSON text into JSON Object.
myData = JSON.parse(text, function (key, value) {
var type;
if (value && typeof value === 'object') {
type = value.type;
if (typeof type === 'string' && typeof window[type] === 'function') {
return new (window[type])(value); <---- Creating the Object and return it.
}
}
return value;
});
OR
myData = JSON.parse(text,reviver(key,value));
function reviver(key, value) {
var type;
if (value && typeof value === 'object') {
type = value.type;
if (typeof type === 'string' && typeof window[type] === 'function') {
return new (window[type])(value); <---- Creating the Object of type of value and return it.
}
}
return value;
}
**===========================**Example of JSON stringifier
* JSON stringifier Converting JSON Object into JSON text (string format).
JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier.
var myJSONText = JSON.stringify(myObject, replacer);
If the stringify method sees an object that contains a toJSON method, it calls that method, and stringifies the value returned. This allows an object to determine its own JSON representation.
The stringifier method can take an optional array of strings. These strings are used to select the properties that will be included in the JSON text.
The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.
Values that do not have a representation in JSON (such as functions and undefined) are excluded.
Nonfinite numbers are replaced with null. To substitute other values, you could use a replacer function like this:
function replacer(key, value) {
if (typeof value === 'number' && !isFinite(value)) {
return String(value);
}
return value;
}
======================================
----------------------------------------
*****************************************
No comments:
Post a Comment