The difference between Eval and parse in JSON

Source: Internet
Author: User

parsing methods for JSON (non-original)

There are two ways to parse JSON: Eval_r () and Json.parse (), using the following methods:

var jsondata = ' {' data1 ': ' Hello, ', ' data2 ': ' world!} ';

var evaljson=eval_r (' (' +jsondata+ ') ');

var jsonparsejson=json.parse (Jsondata);

This converts the Jsondata JSON-formatted string into a JSON object.

The differences are as follows:

var value = 1;

var jsonstr = ' {' data1 ': "Hello", "data2": ++value} ';

var data1 = Eval_r (' (' +jsonstr+ ') '); Console.log (data1);//This value is 2vardata2=json.parse (JSONSTR); Console.log (data2 );//Error

You can see the results of the control output, the first eval_r () successfully executed, the second error.

As can be seen from the above example, eval will execute the code in the string when parsing the string (the consequences are pretty bad), as in the previous example, the value of the original values has been changed by using Eval to parse a JSON string.

The difference between Eval and parse in JSON

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.