Javascript JSON eval Comprehension

Source: Internet
Author: User

EVOperating principle of the Al Function

EVThe Al function evaluates a givenBytesCodeAnd tries to execute the expressions contained in the string or a series of legal javasExplain statement. EVThe Al FunctionLastThe value or reference contained in an expression or statement as the return value.

Example

    • EVAl evaluate javasLimit expression
VaRBar= 'Bar';
VaRFoobar= Eval('"Foo" + bar');
Alert(Foobar);

    • EVAl evaluate javasExplain statement
 VaR Bar =   'Bar'  ;  
// If variable bar equals 'bar', foobar is the result
// Last executing statement: bar = "foo-bar ";
VaR Foobar = Eval ( 'If (bar = "bar") {bar = "foo-bar";} else {bar = "Bar-foo ";}' );
Alert ( Foobar);
// Change the value
Bar = 'Foo' ;
// Now our the last executed statement is: bar = "Bar-foo ";
// Therefore the value of variable foobar has been changed
// Into 'bar-foo'
Foobar = Eval ( 'If (bar = "bar") {bar = "foo-bar";} else {bar = "Bar-foo ";}' );
Alert ( Foobar );

JSON format

JSON format is composed of braces and name-value pairs consisting of colons. Note the difference between JSON format and object literal (Object literals): The JSON name is strictly represented by quotation marks + name.

Example

    • Object literal
  var  objectliteral = { 
name : "objector. L " ,
age : "24" ,
special : "JavaScript" ,
sayname : function () {
return This . name ;
}
} ;

    • JSON object
 VaR Jsonformat =   {
"Summary" : "Blogs" ,
"Blogrolls" : [
{
"Title" : "Explore JavaScript" ,
"Link" : "Http://example.com /"
} ,
{
"Title" : "Explore JavaScript" ,
"Link" : "Http://example.com /"
}
]
} ;

EVAl and JSON

Due to the rise of Ajax, the lightweight JSON data format is gradually becoming popular as the transmission format between the client and the server, then the question arises: how to convert the JSON Data Built on the server to available javasUpload object. Using EVThe Al function is undoubtedly a simple and direct method. During conversion, enclose the JSON string with parentheses:

VaRJsonobject= Eval("(" +Jsonformat+ ")");

Why are Brackets added?

the purpose of adding parentheses is force EV Al function evaluates javas force the expression (expr ession) is converted to an object, instead of being executed as a statement . For example, if no outer brackets are added to the object literal, ev Al recognizes braces as the start and end mark of the javas quit code block. Then, {} is considered to have executed an empty statement. The following two execution results are different:

Alert(Eval("{}");// Return undefined
Alert(Eval("({})");// Return object [object]

Why?The JSON format name is enclosed in quotation marks.?

Because EVThe Al function interprets {FOO: "Bar"} as a legal javas.Explain statement, not an expression. But what people often want is to make EVAl interprets this code as an object. Therefore, the JSON format willAdd quotation marks and parentheses to the outer of the name, so that EVAl will not interpret JSON as a code block..

Example

    • EVAl error parsing Semantics
Alert(Eval('{FOO: "bar "}'));// Return "bar", incorrect

    • EVAl parses JSON correctly
  
  alert   (  eval   (  '({"foo ": "bar"}) ' );   // return JSON object, correct  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.