Eval, json. parse () Introduction and usage notes, evaljson. parse

Source: Internet
Author: User

Eval, json. parse () Introduction and usage notes, evaljson. parse
Define and use the eval () function to calculate a string and execute the JavaScript code. Syntax: the eval (string) parameter description string is required. The string to be calculated, which contains the JavaScript expression to be calculated or the statement to be executed. The return value is the string value (if any ). This method only accepts the original string as the parameter. If the string parameter is not the original string, this method will be returned without any change. Therefore, do not pass a String object as a parameter for the eval () function. If you try to override the eval attribute or assign the eval () method to another attribute and call it through this attribute, ECMAScript can throw an EvalError exception. Eg: <script type = "text/javascript"> eval ("x = 10; y = 20; document. write (x * y) ") document. write (eval ("2 + 2") var x = 10document. write (eval (x + 17) </script> output: 200427 processes json data. 1. eval processes json data in different formats, there are two methods to convert json data into json data. The method is var dataObj = eval ("(" + json + ")"); 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 as statements) to convert json data to json objects (1) json data with "nodes" 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 var json = {"name": "1", "name": "2", "name ": "3"} use $ (dataObj ). each (function (I, item) {var str = "name:" + item. name + ", value:" + item. value;}) obtain the corresponding data. Note: eval is dangerous to use, so it is not recommended to use it if it is not necessary, so as not to add unnecessary troubles. 2. JSON. parse (String, function () Strign: Required parameter. It is a json data function (): optional parameter. If it is not null, then, each member of the object calls the word function and returns an object or an array. This method can achieve the same effect as eval, which is simple and easy to use, however, this method does not support standard IE6 and IE7 models, but you can download json2.js to solve this problem: http://www.JSON.org/json2.js Usage: var dataObj = JSON. parse (json); for (var data in dataObj) {// obtain the value of the corresponding data}

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.