JS in Json.stringify () and Json.parse () and eval () detailed and use case

Source: Internet
Author: User

JSON (JavaScript Object Notation) is a lightweight data interchange format. Because of the language-independent text format, also used similar to the C language family habits, with these features make JSON known as the ideal data exchange language, the role is easy to read and write, but also easy to machine parsing and generation (generally used for network transmission rate).

One: Json.parse ();

Function: Converts a JSON string into a JSON object

Syntax: JSON. Parse (Text[,reviver]).

Parameters:

Text: Required, a valid JSON string.

Reviver: Optional.

Return value: An object or an array.

    <script type= "Text/javascript" >        var jsonstr = ' {' name ': ' Huangxiaojian ', ' age ': ' + '} ';         var jsonobj = json.parse (jsonstr);        Console.log (Jsonobj); {Name: "Huangxiaojian", Age: "~"}    </script>

II: Json.stringify ();

Function: Converts a JSON object into a JSON string.

Syntax: json.stringify (value [, Replacer] [, space]).

Parameters:

Value: Required, usually an object or an array.

Replacer: Optional, function or array for converting results.

Space: Optional, add indents, spaces, and line breaks to the return value JSON text to make it easier to read.

Return value: A string containing the JSON text.

    <script type= "Text/javascript" >        var jsonobj = {"Name": "Zhang San", "Age": "23 years old"}        ; var jsonstr = json.stringify (jsonobj);         // {"name": "Zhang San", "Age": "23 years old"}    </script>

Three: eval ();

Function: the eval () function evaluates a string and executes the JavaScript expression in it or the statement to be executed.

Syntax: eval (string).

Argument: string must be a string that needs to be evaluated, which contains the JavaScript expression to be evaluated or the statement to execute.

Return value: Returns the value of the computed string without making any changes.

    <script type= "Text/javascript" >        eval (' x = ten; y = 5; Console.log (x*y) ');                Console.log (eval (' 5+5 '));                 var x = ten;        Console.log (eval (' x + 5 '));     </script>

Using the eval () function can also parse a JSON string into an object, which accomplishes the function of Json.parse (), but in a different way, see the following code:

    <script type= "Text/javascript" >        var str = ' {' name ': ' Huangxiaojian ', ' age ': ' + '} ';          var info = json.parse (str);         // Object {name: "Huangxiaojian", Age: "All"}        var info = eval (' (' + str + ') ');         // Object {name: "Huangxiaojian", Age: "All"}    </script>

  

It is noted that eval () also uses a pair of parentheses to wrap the string together, and I find a better explanation for this:

Cause: Due to the question of Eval itself, because JSON is started and ended in the way "{}", in JS, it is treated as a block of statements, so it must be coerced into an expression.

Workaround: 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, rather than being executed as a statement (statement). For example, if the object literal {} is not enclosed, then eval will recognize the curly brace as the start and end tag of the JavaScript block, and {} will be considered an empty statement. Take a look at the different examples below:

    <script type= "Text/javascript" >        console.log (eval (//  undefined        //  Object {}    </script>

JS in Json.stringify () and Json.parse () and eval () detailed and use case

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.