js json the serialization and deserialization
(1) serialized
Js i.e. into a string in the Object
1. toJSONString
var last = obj.toJSONString (); // JSON object into the character JSON
2. Use stringify
var last = JSON.stringify (obj); // JSON object into the character JSON
(2) deserialization
I.e. JSON string into the js Object
1. Using parse
var obj = JSON.parse (data); // Converts a string to a JSON object JSON
2. Use parseJSON
var obj = data.parseJSON (); // Converts a string to a JSON object JSON
3. Use eval
var obj=eval("("+data+")");
Why eval here to add "(" + data + ") ; //" it?
The reason is: eval problem itself. Since json is "{}" approach to the beginning and the end, in JS, it will be processed as a block of statements, so it must be converted into a compulsory expression.
(1) serialized
Js i.e. into a string in the Object
1. toJSONString
var last = obj.toJSONString (); // JSON object into the character JSON
2. Use stringify
var last = JSON.stringify (obj); // JSON object into the character JSON
(2) deserialization
I.e. JSON string into the js Object
1. Using parse
var obj = JSON.parse (data); // Converts a string to a JSON object JSON
2. Use parseJSON
var obj = data.parseJSON (); // Converts a string to a JSON object JSON
3. Use eval
var obj=eval("("+data+")");
Why eval here to add "(" + data + ") ; //" it?
The reason is: eval problem itself. Since json is "{}" approach to the beginning and the end, in JS, it will be processed as a block of statements, so it must be converted into a compulsory expression.