The connection between JavaScript eval and JSON

Source: Internet
Author: User
Tags javascript eval

Original address:http://www.jb51.net/article/21688.htm This article focuses on the relationship between the Eval function and the JSON data format, as well as some detail issues. If you would like to learn more about Eval and JSON, please refer to the following link: eval:https://developer.mozilla.org/en/core_javascript_1.5_reference/global_functions/ How the eval json:http://www.json.org/eval function works The Eval function evaluates a given string containing JavaScript code, and tried 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. An example of an 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 of//Last executing statement:bar= "Foo-bar"; var fo Obar = 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 V Alue of variable foobar have been changed//into ' bar-foo ' Foobar = 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 of object literals
var objectliteral = {  name: "Objector.l", Age  : "$",  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 because of the rise of Ajax, the lightweight data format of JSON is becoming popular as a transport format between client and server, and the problem is how to convert 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 Undefinedalert (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. Example of eval error parsing semantics
Alert (eval (' {foo: ' Bar '} '));   Return "Bar", incorrect

Eval correctly parses JSON

Alert (eval (' ({"foo": "Bar"})); Return JSON object, correct
Conclusion understand how Eval works and the strict format of JSON, combining the data transfer and object transformation of eval and JSON applied to JavaScript. Following this format:
Eval (' (' + jsonstring + ') ');

The connection between JavaScript eval and JSON

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.