Introduction and use of Eval, Json.parse () Attention points

Source: Internet
Author: User

Original: eval, Json.parse () Introduction and use of attention points


Use of Eval and Json.parse
Eval: From the official website definition and usage the eval () function computes a string and executes the JavaScript code in it. Syntax: eval (string) parameter Describe string Necessary. The string to evaluate that contains the JAVASCRIPT expression to be evaluated or the statement to execute. Returns the value, if any, obtained by calculating a string. Indicates that the method accepts only the original string as an argument, and if the string parameter is not the original string, the method returns without any change. Therefore, do not pass a String object as a parameter for the eval () function. If you attempt to override the Eval property or assign the eval () method to another property and call it through the property, the ECMAScript implementation allows a Evalerror exception to be thrown.
Eg:<script type= "Text/javascript" >eval ("X=10;y=20;document.write (x*y)") document.write (eval ("+ +)") var x= 10document.write (eval (x+17)) </script> output: 200427 processing JSON data 1, eval depending on the JSON format, there are generally two ways JSON data is converted to JSON data, Use the unified var dataobj = eval ("+json+"), and the purpose of the parentheses is to force the Eval function to force the expression in parentheses to be converted to an object while processing the JavaScript code. Instead of as a statement (statement) to perform the conversion of JSON data to a JSON object (1) A JSON data containing "node" var json = {root: [{name: ' 1 ', Value: ' 0 '}, {name: ' 2 ', Value: ' 1 '}, {Name: ' 3 ', Value: ' 2 '}, '} '; If you get the value of the corresponding name and value, you can use $ (dataobj.root). each (function (I,item) { var str = "Name:" +item.name+ ", Value:" +item.value;}) (2) JSON data that does not contain "node" var json = {"Name": "1", "Name": "2", "Name": "3"} using $ (dataobj). each (function (I,item) { var str = "Name:" +item.name+ ", Value:" +item.value;}) Get the corresponding data note: Eval use is more dangerous, can not use the best, so as not to increase unnecessary trouble

2, Json.parse (String,function ()) Strign: Required parameter, JSON data function (): optional parameter, if not NULL, the word function is called for each member of the object
The return result is an object or an array, this method with eval can achieve the same effect, simple and easy to use, but this method does not support IE6, IE7 Standard mode, but you can download json2.js to solve this problem URL: http://www.JSON.org/json2.js
How to use: var dataobj = Json.parse (JSON);
for (var data in dataobj) { Gets the value of the corresponding data}

Introduction and use of Eval, Json.parse () Attention points

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.