JavaScript eval Parse JSON string _ Basics

Source: Internet
Author: User
Tags arithmetic eval javascript eval

As we all know, advanced browsers can use the Json.parse () API to parse a JSON string into JSON data, slightly defective, and we can use the eval () function.

var str = ' {' name ': ' Hanzichi ', ' age ': ';
var obj = eval (' (' + str + ') ');
Console.log (obj); Object {name: "Hanzichi", age:10}

Do you notice that the STR variable wraps around a bracket when it is passed to eval ()? Why did you do that?

Let's take a look at the definition and use of the Eval function.

The argument for Eval () is a string. If the string represents an expression, eval () is evaluated for the expression. If the parameter represents one or more JavaScript declarations, then Eval () executes the declaration. Do not call Eval () to evaluate an arithmetic expression; JavaScript will automatically evaluate the arithmetic expression.

Simply put, the Eval function's argument is a string, and if the string is "nostring", it will be a normal JavaScript statement that can be run.

What do you say? Give me a chestnut, the following code:

var str = "alert (' Hello World ')";
eval (str);

"Hello World" pops up after execution. We have the str variable "nostring", the rough way is to remove the outside quotation marks, internal adjustment (escape, etc.), and then become:

Alert (' Hello World ')

Very good! This is the normal JavaScript statement that can be run! Run the!

To get back to the beginning of the question, why is the JSON string wrapped in parentheses. If not, it looks like this:

var str = ' {' name ': ' Hanzichi ', ' age ': ';
var obj = eval (str); Uncaught syntaxerror:unexpected Token:

Yes, it's an error. Why do you have an error? Try the str "nostring" and execute it:

{"Name": "Hanzichi", "Age": ten}; 
Uncaught syntaxerror:unexpected Token:

There is no doubt that a JSON object or an object is not a JavaScript statement that can be executed at all! Wait, try the following code:

var str = ' {name: ' Hanzichi '} ';
var obj = eval (str);
Console.log (obj); Hanzichi

What the hell is this? But add "" to the name and the error?

var str = ' {' name ': ' Hanzichi '} ';
var obj = eval (str); Uncaught syntaxerror:unexpected token:
console.log (obj);

Well, it's getting dizzy, but you can still turn str "nostring" to see if it's a JavaScript statement that executes correctly. The result of the former is:

{name: ' Hanzichi '}

This is indeed a legitimate JavaScript statement. {} We can use not only in scenarios such as if, for statements, or even at any time, because JavaScript has only block-level scopes before ES6, so there's no conflict about scopes or anything. Remove {} name: "Hanzichi" is also a legitimate statement, a label statement, the label statement in the loop out of the nesting is very useful, you can refer to the label, and as a label statement, name can not be quoted, the mark could be placed in the JAVASCRI PT code anywhere, not to use it doesn't matter.

Once an object has two key, such as {name: "Hanzichi", age:10}, OK, two label statements? Consider "Hanzhichi" and 10 respectively as statements, but the statements can only be connected by a number of letters! (commas can be used between expressions). So it's no problem to change to the following:

var str = ' {name: ' Hanzichi '; age:10} ';
var obj = eval (str); 
Console.log (obj); 10

The farther away, the reason for the error of the code at the beginning of the article is found, why the parentheses can be solved? In simple terms, () converts a statement into an expression called a statement expression. The code in parentheses is converted to an expression evaluation and returned, and the object literal must exist as an expression.

This article does not talk about expressions, it is worth remembering that expressions always have a return value. Most expressions are wrapped in (), the parentheses cannot be empty, and if there are multiple expressions, separated by commas, the so-called comma expressions, the last value is returned.

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.