Eval usage the eval function receives a parameter s. If s is not a string, s is directly returned. Otherwise, execute the s statement. The following is an example of eval parsing json. The eval usage is simple and can be skipped if you are familiar with it.
The eval function receives a parameter s. If s is not a string, s is directly returned. Otherwise, execute the s statement. If the execution result of the s statement is a value, this value is returned; otherwise, undefined is returned. Note that the object declaration Syntax "{}" does not return a value and must be enclosed in parentheses to return the value. A simple example is as follows:
The Code is as follows:
Var s1 = '"a" + 2'; // expression
Var s2 = '{a: 2}'; // statement
Alert (eval (s1); //-> 'a2'
Alert (eval (s2); //-> undefined
Alert (eval ('+ s2 +'); //-> [object Object]
As you can see, for object declaration statements, they are only executed and cannot return values.
To return a commonly used object declaration statement such as "{}", it must be enclosed in parentheses to convert it into an expression before returning its value. This is also one of the basic principles of Ajax development using JSON. In the example, we can clearly see that the second alert statement outputs undefined, and the third statement adds brackets to output the object represented by the statement.
The focus of this article is how to execute global code in the function. To illustrate this problem, let's first look at an example:
The Code is as follows:
Var s = 'global'; // defines a global variable.
Function demo1 (){
Eval ('var s = "local "');
}
Demo1 ();
Alert (s); //-> global
Well understood, the above demo1 function is equivalent to: function demo1 () {var s = 'local' ;}, which defines a local variable s.
So the final output is global. After all, everyone can clearly distinguish between local variables and global variables.
After careful consideration, we can find that the eval function is always executed in the context variable space (also called package, closure) that calls it, both the variable definition and the function definition are the same, so the following code will generate an undefined function error:
The Code is as follows:
Var s = 'function test () {return 1 ;}'; // a function Definition Statement
Function demo2 (){
Eval (s );
}
Demo2 ();
Alert (test (); //-> error: test is not defined
This is because the test function is defined in a local space and can be accessed in the demo2 function.
Share: Notes for parsing JSON using Js eval
JSON strings are parsed into JSON data formats in JS. There are two methods:
1. Use the eval () function.
2. Use the Function object for return parsing.
Use the eval function to parse and use the jquery each method to traverse
The JSON data parsing method using jquery serves as the transmission object of jquery asynchronous requests. After a jquery request is sent, the returned result is a json object. Here we consider the form of a JSON string returned by the server, JSON objects encapsulated by plug-ins such as JSONObject are similar in that they are not described here.
Here we first provide a JSON string set, which is as follows:
The Code is as follows:
Var data ="
{
Root:
[
{Name: '1', value: '0 '},
{Name: '000000', value: 'beijing '},
{Name: '123', value: 'tianjin '},
{Name: '000000', value: 'shanghai '},
{Name: '000000', value: 'chongqing '},
{Name: '200', value: 'weinan City '},
{Name: '000000', value: 'yan'an '},
{Name: '000000', value: 'hanzhong '},
{Name: '200', value: 'yulin '},
{Name: '123', value: 'ankang City '},
{Name: '200', value: 'shangluo City '}
]
}";
Here, based on the data type obtained asynchronously by jquery-json object and string, we will introduce the Processing Methods of the obtained results in two ways.
1. for the JSON string returned by the server, if the jquery asynchronous request does not provide a type description or is accepted as a string, an object processing is required. The method is not too troublesome, but the string is placed in eval (). This method is also suitable for obtaining json objects in the common ccipt mode. The following is an example:
Var dataObj = eval ("(" + data + ")"); // convert to a json object
Why do you want to add "(" ("+ data +") "); //" to eval?
The reason is: Problems with eval itself. Json starts and ends in the form of "{}". In JS, json is treated as a statement block. Therefore, it must be forcibly converted into an expression.
The purpose of parentheses is to force the eval function to forcibly convert the expressions in parentheses into objects when processing JavaScript code, rather than being executed as statements. For example, if no outer brackets are added to the object literal {}, eval identifies the braces as the start and end mark of the JavaScript code block, then {} is considered to have executed an empty statement. The following two execution results are different:
The Code is as follows:
Alert (eval ("{}"); // return undefined
Alert (eval ("({})"); // return object [Object]
For this method, we can see it everywhere in JS.
For example, (function () {} (); During the closure operation.
The Code is as follows:
Alert (dataObj. root. length); // output the number of root sub-objects
$. Each (dataObj. root, fucntion (idx, item ){
If (idx = 0 ){
Return true;
}
// Output the name and value of each root sub-Object
Alert ("name:" + item. name + ", value:" + item. value );
})
Note: for General js json objects, you only need to replace the $. each () method with the for statement.
2. for the JSON string returned by the server, if the jquery asynchronous request sets the type (usually this Configuration Attribute) to "json", or the $. the getJSON () method does not need the eval () method to obtain the server response. Because the result is already a json object, you only need to call this object directly. In this example, $. the getJSON method is used as an example to describe the data processing method:
The Code is as follows:
$. GetJSON ("http://www.xx.cn/", {param: "gaoyusi"}, function (data ){
// The returned data is already a json object.
// The following operations are the same as the first case
$. Each (data. root, function (idx, item ){
If (idx = 0 ){
Return true; // returns the same value as countinue, and returns the same value as break.
}
Alert ("name:" + item. name + ", value:" + item. value );
});
});
Note that the eval () method in method 1 dynamically executes the strings (which may be JavaScript scripts), which can easily cause system security problems. Therefore, some third-party client script libraries that circumvent eval () can be used. For example, JSON in JavaScript provides a script library of no more than 3 K.
The second method of parsing is to use the Function object. Its typical application is to parse the data returned by success under the AJAX method in JQUERY.
The Code is as follows:
Var json = '{"name": "CJ", "age": 18 }';
Data = (new Function ("", "return" + json ))();
At this time, data is parsed into a json object.