If you would like to learn more about Eval and JSON, please refer to the following links:
Eval:https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Functions/Eval
JSON:http://www.json.org/
How the Eval function works
The Eval function evaluates a given string containing JavaScript code and attempts to execute an expression contained in a string or a series of legitimate JavaScript statements. The Eval function takes the last expression or the value or reference contained in the statement as the return value.
Examples Show
- Eval evaluation JavaScript expression
var bar=' Bar '; var foobar = eval ( "foo" + Bar ' ) Alert (Foobar);
- Eval evaluation JavaScript statement
var bar=' Bar ';If variable bar equals ' bar ', foobar is the result ofLast executing statement:bar= "Foo-bar";var foobar=Eval(' if (bar = = "Bar") {bar= "Foo-bar";} else {bar = "Bar-foo";} ');Alert(Foobar);Change the Valuebar=' foo ';Now we the last executed statement is:bar = "Bar-foo";Therefore the value of variable foobar has been changed//into ' Bar-foo ' foobar Span class= "Sh_symbol" style= "color: #8b0000; line-height:20px; " >= eval ( ' if (bar = = "Bar") {bar= "Foo-bar";} else {bar = "Bar-foo";} ' ) Alert (Foobar);
Format of JSON
The format of JSON is composed of curly braces and name-value pairs consisting of colons (:). Note the difference between the JSON format and the object literal literals: The JSON name is strictly denoted by the quotation mark + name.
Examples Show
- The literal of the object
var objectliteral={Name:"OBJECTOR.L", age:" the", Special:"JavaScript", Sayname: function () {return this.name}};
var Jsonformat={"Summary":"Blogs","Blogrolls":[{"title":"Explore JavaScript","link":"http://example.com/" },{"title": , : "http://example.com/" ]};
Eval and JSON
Thanks to the advent of Ajax, the lightweight data format of JSON is becoming increasingly popular as a transport format between client and server, and the problem is how to translate the server-side JSON data into usable JavaScript objects. Using the Eval function is undoubtedly a simple and straightforward approach. You need to wrap a layer of parentheses around the outside of the JSON string when converting:
var jsonobject = eval ( + Jsonformat + ) ;
Why do you want to add parentheses?
The purpose of the parentheses is to force the Eval function to force the expression in parentheses to be converted to an object when evaluating the JavaScript code, rather than being executed as a statement (statement). For example, if the object literal {} is not enclosed, then eval will recognize the curly brace as the start and end tag of the JavaScript block, and {} will be considered an empty statement. So the following two execution results are different:
Alert(Eval("{}"); //return Undefined Alert (eval ( ); //return Object[object]
Why is the name part of the JSON format quoted?
Because the Eval function interprets {foo: "Bar"} as a legitimate JavaScript statement, not an expression. But what people often want is for eval to interpret this code as an object. So the JSON format will force you to add quotation marks to the outside of the name and combine parentheses so that Eval does not mistakenly interpret the JSON as a block of code.
Examples Show
- Eval Error parsing semantics
Alert(eval(' {foo: ' Bar '} '//Return ' bar ', incorrect
- Eval correctly parses JSON
alert (eval ( )); //return JSON object, Correct
Conclusion
Understand how Eval works and the rigorous qualifying format of JSON, combining Eval and JSON with data transfer and object transformations applied to JavaScript.
Following this format:
Eval(' {') ');
How the Eval function works