Relationship between javascript eval and JSON

Source: Internet
Author: User
Tags javascript eval

How eval functions work

The eval function evaluates a given string containing javascript code, and the view executes the expressions or

A series of legal javascript statements. The eval function uses the value or reference contained in the last expression or statement as the return value.

console.info(eval("alert('a');alert('b')"))console.info(eval("x=10;y=20;alert(x*y)"));   //200console.info(eval("3+6"));   //9

JSON format
Json is simply an object and an array in javascript. Therefore, the two structures are objects and arrays. These two structures can represent various complex structures.
1. Object: Content enclosed by the object represented as "{}" in js. The data structure is {key: value, key: value ,...} the structure of the key-value pair. In the object-oriented language, the key is the object attribute, and the value is the corresponding attribute value, so it is easy to understand. The value method is the object. key, which can be a number, String, array, or object.
2. array: The array is enclosed by brackets "[]" in js. The data structure is ["java", "javascript", "vb",...], the value can be obtained using indexes in the same way as in all languages. Field values can be of the following types: Numbers, strings, arrays, and objects.
After the object and array structures, you can combine them into complex data structures.

{"AcceptIs": null, "entity": null, "refuseIs": null, "result": {"pageSize": 10, "resultList": [{"SHNOTE ": "Ferrari F40", "STATE": "Submit", "CTIME": "13:53:28", "PHONE": "13111050937", "ID ": "40288ba54000028000014000036c6c60000",},], "totalPage": 1, "totalSize": 4 }}

Eval and JSON
The eval function can be used to convert JSON data into available javascript objects.

var datas=eval("("+data+")");

Why should we add parentheses?
The purpose of parentheses is to enable the eval function to forcibly convert the expressions in parentheses into objects when evaluating javascript code,
Instead of being executed as a statement. For example, if the object {} does not contain outer brackets, eval identifies the braces as the start and end marks of the javascript code block, then {} is considered to have executed an empty statement. As follows:

console.info(eval("{}"));   //undefined console.info(eval("({})")); //Object {}

Project Code involved:

$(document).ready(function(){$.post('/homea/admin/UserBookView.do', {}, function(data) {    var datas=eval("("+data+")");for(var i=0;i
 
  

Related Article

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.