In-depth analysis of the functions of JSON. parse (), JSON. stringify () and eval (), json. stringifyeval

Source: Internet
Author: User
Tags javascript eval

In-depth analysis of the functions of JSON. parse (), JSON. stringify () and eval (), json. stringifyeval

"JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of ECMAScript. Because it adopts a language-independent text format and also uses a habit similar to the C language family, with these features, JSON becomes the ideal data exchange language, which is easy to read and write, it is also easy for machine parsing and generation (generally used to increase the network transmission rate )."

Here I want to briefly talk about the JSON. parse () and JSON. stringify () functions in jquery. By the way, I will also mention the eval () functions in native JS.

(1) JSON. parse function

Purpose: convert a JavaScript Object Notation (JSON) string to an object.

Syntax: JSON. parse (text [, reviver])

Parameters:

Text is required. A valid JSON string.

Reviver is optional. A function of the conversion result. This function will be called for each member of the object.
Returned value: an object or Array

Example:

Var json = '{"name": "GDT", "age":, "University": "GDUT"}'; var info = JSON. parse (json); // parses it into a JSON object document. write (info. name + 'is a student of '+ info. university + 'and he is' + info. age + "years old. ");/info is the Object

(2) JSON. stringify () function

Purpose: Convert JavaScript values to JavaScript Object Notation (JSON) strings

Syntax: JSON. stringify (value [, replacer] [, space])

Parameters:

Value is required. It is usually the JavaScript value to be converted (usually an object or an array)

(Optional) replacer. It is a function or array used to convert the result.

Space is optional. Add indentation, spaces, and line breaks to the returned JSON text to make it easier to read.

Returned value: a string containing JSON text

Example:

Var info = {name: "GDT", age:, University: "GDUT"}; var json = JSON. stringify (info); // converts it to a JSON string document. write (json); // output is {"name": "GDT", "age": 23, "University": "GDUT "}

(3) eval () function

Purpose: The eval () function can calculate a string and execute the JavaScript code.

Syntax: eval (string)

Parameters:

String is required. the string to be calculated contains the JavaScript expression to be calculated or the statement to be executed.

Return Value: return the string value. If yes (if no value is returned, no changes are made)

Example:

Eval ("x =; y =; document. write (x * y) "); // output is document. write (eval ("+"); // output is var x =; document. write (eval (x +); // output is

You can use the eval () function to parse the JSON string into an object. This function can complete the JSON. parse () function, but there are some differences. See the following code.

// JSON. parse () var json = '{"name": "GDT", "age":, "University": "GDUT"}'; var info = JSON. parse (json); // parses it into a JSON object document. write (info); // output is [object Object] // eval () var json = '{"name": "GDT", "age":, "University ": "GDUT"} '; var info = eval (' + json + '); // parses it into a JSON object document. write (info); // output is [object Object]

I don't know if you have noticed that eval () also needs to wrap the string with a pair of parentheses. I find a better explanation for this:

Cause: it is caused by the eval problem. Because json starts and ends in the form of "{}", it will be processed as a statement block in JS, therefore, it must be forcibly converted into an expression.

Solution: 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 being executed as statements. For example, if no outer brackets are added to the object literal {}, eval identifies the braces as the start and end mark of the JavaScript code block, then {} is considered to have executed an empty statement. See the following examples

alert(eval("{}")); // return undefinedalert(eval('('+'{}'+')')); // return object[Object] 

In addition, compared with JSON. parse (), eval () can parse any string. eval is insecure because eval is loose and may cause security problems. For example, the following code:

Var str = '{"a": "B"}'; document. write (eval ("(" + str + ")"); // The object is parsed as var str = '{"a": (function () {alert ("I can do something bad! ") ;}) ()} '; Eval (' + str + '); // you can run a trojan script.

If a malicious user injects a script to insert a trojan link to the page into a json string, eval can also be used, whereas JSON. parse () does not have to worry about this problem. It can be seen that although eval () is powerful, there are not many opportunities to use it.

My personal summary is now. This is my first blog in my life. It was born on Fool's Day in April 1. I hope you will forgive me for writing poorly. Now the technology is very poor, I hope that I can accumulate more than 1.1 pieces of knowledge to lay a good foundation for future success ~

Articles you may be interested in:
  • Solution to inaccurate date and time when JSON. stringify is converted to JSON
  • Introduction to JSON. parse () and JSON. stringify ()
  • Print the content of the json object and the JSON. stringify Function Application
  • Parse and serialize json data in js (1) basic usage of json. stringify ()
  • Example of JSON. stringify syntax
  • Js parses json using eval (json is used in js)
  • Notes on jquery eval JSON Parsing
  • Jqeury eval converts strings to json
  • Use eval in js to generate a JSON object
  • JSON and JavaScript eval instructions
  • Connection between javascript eval and JSON
  • JavaScript eval JSON object

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.