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

Source: Internet
Author: User

Eval and Json.parse use  eval: derived from official website definition and usage the eval () function computes a string and executes the JavaScript code in it. Syntax: the  eval (String) parameter describes the string required. 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 (x+17) </script> output: 200427 processing JSON data 1, eval depending on the JSON format, there are generally two ways JSON converts JSON data 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 be used best not to use, so as not to increase unnecessary trouble   2, Json.parse (String,function ()) Strign: Required parameters, As JSON data function (): optional parameter, if not empty, invokes the word function for each member of the object   Returns the result as an object or an array, this method has the same effect as Eval and is 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  use method: var dataobj = Json.parse (JSON),  for (var data in dataobj) {//Get the value of the corresponding data}

Introduction to Eval, Json.parse (), and usage notes

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.