JSON (JavaScriptObjectNotation) is a simple data format, which is lighter than xml. The following section describes a small problem of Eval parsing JSON strings in JS, for more information about JSON, see the Introduction to JSON. As we all know, advanced browsers can use JSON. parse () API to parse a JSON string into JSON data. We can use the eval () function if it is slightly inappropriate.
JSON (JavaScript Object Notation) is a simple data format, which is lighter than xml. JSON is a JavaScript native format, which means that no special API or toolkit is required to process JSON data in JavaScript.
JSON rules are simple: the object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs.
var str = '{"name": "hanzichi", "age": 10}';var obj = eval('(' + str + ')');console.log(obj); // Object {name: "hanzichi", age: 10}
Have you noticed that when passing parameters to eval (), the str variable is enclosed in parentheses? Why?
Let's take a look at the definition and use of the eval function.
The eval () parameter is a string. If the string represents an expression, eval () evaluates the expression. If the parameter represents one or more JavaScript declarations, eval () executes the declaration. Do not call eval () to evaluate the arithmetic expression; JavaScript will automatically evaluate the arithmetic expression.
To put it simply, the eval function parameter is a string. If the string "noString" is processed, the JavaScript statement can be run normally.
What do you say? For example, the following code:
var str = "alert('hello world')";eval(str);
After the command is executed, "hello world" is displayed ". The str variable "noString" is removed from the quotation marks, internal adjustments (Escape, etc.), and then changed:
alert('hello world')
Very good! This is a normal JavaScript statement! Run it!
Back to the start Question, why should the JSON string be enclosed in parentheses. If this parameter is not added, it looks like this:
var str = '{"name": "hanzichi", "age": 10}';var obj = eval(str); // Uncaught SyntaxError: Unexpected token :
Well, an error is reported. Why is an error reported? Try str "noString" and execute:
{"name": "hanzichi", "age": 10}; // 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 is this? But an error is returned when "" is added to the name?
var str = '{"name": "hanzichi"}';var obj = eval(str); // Uncaught SyntaxError: Unexpected token :console.log(obj);
Well, it's almost dizzy. In fact, you can still convert str "nostring" to see if the JavaScript statement can be correctly executed. The result of the former is:
{name: "hanzichi"}
This is indeed a legal JavaScript statement. {} We can not only use it in scenarios such as if and for statements, but even at any time, because JavaScript only has block-level scope before ES6, so there is no conflict between scope and anything. After removing {}, name: "hanzichi" is also a legal statement. A label statement is very useful in jumping out of nested loops. For details, see label, as a label statement, name cannot be enclosed by quotation marks, which can be placed anywhere in JavaScript code.
Once an object has two keys, for example, {name: "hanzichi", age: 10}, OK, two label statements? Think of "hanzhichi" and "10" as statements respectively, but the statements can only be connected by a block number! (Comma can be used between expressions ). So it's okay to change it to the following:
var str = '{name: "hanzichi"; age: 10}';var obj = eval(str); console.log(obj); // 10
The reason for the code error at the beginning of the article is to find out. Why can we fix it with parentheses? () Converts a statement into an expression, which is called a statement expression. The code in parentheses is converted to an expression to evaluate the value and return the result. The object literal must exist as an expression.
This article does not talk about expressions. For expressions, refer to the link at the end of this article. It is worth remembering that an expression always has a return value. Most expressions are enclosed in () and cannot be empty in parentheses. If multiple expressions are separated by commas (,), the last value is returned.
The above section introduces a small question about Eval parsing JSON strings in JS. I hope it will help you!