JSON strings and JSON objects convert to and from each other

Source: Internet
Author: User
Tags string to json

What is JSON?
    1. JSON refers to JavaScript Object notation (JavaScript Notation)
    2. JSON is a lightweight text data interchange Format
    3. JSON Independent of Language *
    4. JSON is self-descriptive and easier to understand

* JSON uses JAVASCRIPT syntax to describe data objects, but JSON is still independent of language and platform. JSON parser and JSON library support many different programming languages

JSON string:

var str = ' {' name ': ' Zwq ', ' sex ': ' Man '} ';

JSON object:

var obj= {"Name": "Zwq", "Sex": "Man"};

JavaScript native parsing (JSON string-->json object)

The JSON text format is syntactically identical to the code that creates the JavaScript object.

Because of this similarity, the JavaScript program can use the built-in eval () function and the function object to generate native JavaScript objects with JSON data without a parser.

    1. 1. eval ();

var obj=eval ("(" +str+ ")");//Convert to JSON object

    1. 2. Function object ;

var obj= (New Function ("", "Return" +str)) (); Convert to JSON object

JSON object parsing
    1. 1. JSON string--- json object


Json.parse (text[, Reviver])

Parameter description:

Text: Required, a valid JSON string.

Reviver: Optional, a function that transforms the result that will call this function for each member of the object

return value :

Returns the converted object for the given JSON string.

Example :

var obj=json.parse (str);

    1. 2. JSON Object--- json string


Json.stringify (value[, replacer[, space])

Parameter description:

Value: Required, a valid JSON object.

Replacer: Optional. The function or array used to convert the result.

If Replacer is a function, Json.stringify calls the function and passes in the key and value of each member. Use the return value instead of the original value. If this function returns undefined, the member is excluded. The key of the root object is an empty string: "".

If Replacer is an array, only the members with key values in the array are converted. The members are converted in the same order as the keys in the array. When the value parameter is also an array, replacer arrays are ignored.

Space: Optional, text adds indents, spaces, and line breaks, if space is a number, the return value text indents the specified number of spaces at each level, and if space is greater than 10, the text indents 10 spaces. Space has the ability to use non-numeric, such as: \ T.

return value :

Returns the converted string for the given JSON object.

Example :

var str=json.stringify (obj);

JSON Conversion Toolkit

To facilitate the processing of JSON data, JSON provides the Json.js package: http://www.json.org

    1. 1. JSON string--- json object

var obj = Str.parsejson (); Convert from JSON string to JSON object

    1. 2. JSON Object--- json string

var str=obj.tojsonstring (); To convert a JSON object to a JSON string

JSON strings and JSON objects convert to and from each other

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.