JSON .parse()

JSON typically used to exchange data with the server.

When the server receives the character string data is typically.

 The JSON.parse () method to convert the data to a JavaScript object.

After the completion of the analysis, you can use JSON data on a web page

JSON.parse(text[, reviver])
  • text: Required, a valid JSON string.
  • reviver:  Optional, a function conversion result, each member will call this function object.

JSON can not store a Date object.

If you need to store the Date object, you need to convert it to a string.

After the string is then converted Date object.

var text = '{ "name":"b", "initDate":"2013-12-14", "site":"www"}';
var obj = JSON.parse(text);
obj.initDate = new Date(obj.initDate);
Enable JSON.parse the second parameter reviver, a function of the result of the conversion, each member object of this function is called. 

var
text = '{ "name": "the Run", "initDate": "2013-12-14", "Site": "COM"}' ; var obj = the JSON.parse (text, function (Key, value) { IF (Key == "initDate" ) { return new new a Date (value); } the else { return value; }});

eval (string): calculate a string function, and executes the JavaScript code.

the eval ( "var a = 1" ); // declare a variable assignment and a 1. the eval ( "2 +. 3" ); // add operation performed, and returns the computed value. eval ( "mytest ()" ); // execute mytest () function. the eval ( "{B: 2}" ); // declare an object.           

For JSON string returned by the server, if the request is not done jQuery asynchronous type described, or as a string accepted, then processing needs to be done once the object is placed in the string eval () is performed once. This approach is also suitable for obtaining json object for ordinary javascipt way:

var u = eval('('+user+')');
alert(eval("{}"); // return undefined alert(eval("({})");// return object[Object]
 

Guess you like

Origin www.cnblogs.com/hpwd/p/11422557.html