JSON-Common conversions

Source: Internet
Author: User

Simply record the use of the Json.parse () and json.stringify () functions in jquery, and the eval () function in JS

1,json.parse function (Common)

Function: Converts a JavaScript object notation (JSON) string to an object.

Grammar:JSON.parse(text [reviver])

Parameters:

    • text required. a valid JSON string.
    • Reviver Optional. a function that transforms the result. This function is called for each member of the object.

Return value: An object or an array

Example:

var json = ' {' name ': ' YZ ', ' age ': ' University ': ' Sjzxy '} '; var info = json.parse (JSON); Resolves to a JSON object document.write (Info.name + ' is a student of ' + info. University + ' and he is ' + Info.age + "Years"); /info to Object

2,json.stringify () function

Role: Converts JavaScript values to JavaScript Object Notation (JSON) strings

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

Parameters:

    • Value required, typically a JavaScript value (usually an object or an array) that needs to be converted
    • Replacer optional, function or array for converting results
    • Space is optional. adds indents, spaces, and line breaks to the return value JSON text to make it easier to read.

Return value: A string containing JSON text

Example

var info = {Name: "YZ", age:22,university: "Sjzxy"}; var json = json.stringify (info); Convert to JSON string
document.write (JSON); Output is {"name": "GDT", "Age": "University": "Gdut"}

(3) eval () function

Function: the eval () function computes a string and executes the JavaScript code in it.

Syntax:eval (String)

Parameters:

    • String required to be evaluated, containing the JAVASCRIPT expression to be evaluated or the statement to be executed.

Return value: Returns the value of the computed string, if any (no change is returned)

Example

Eval ("X=11;y=2;document.write (X*y)");  Output is document.write (eval ("+ +)"); Output is 4 var x=10;  document.write (eval (x+17)); Output is 27
The format of the eval () parsing string
Alert (eval (' (' + ' (' + ') ' {} ') ') but Eval () is unsafe for easy execution of dangerous code inside
Example:
var str2 = ' {' A ': (function () {alert ("I can do something bad!");}) ()}‘;
Eval (' (' +str2+ ') ');
The code that executes the danger is unsafe  but json.parse () completely overcomes this problem, so the actual use of less

--Citation of Internet information if infringement is deleted


JSON-Common conversions

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.