Use JSON. stringify () to generate a JSON string in beautiful format, json. stringifyjson

Source: Internet
Author: User

Use JSON. stringify () to generate a JSON string in beautiful format, json. stringifyjson
Clever UseJSON. stringify ()Generate a JSON string in beautiful format

Processing XML using JavaScript is basically a cup of cake, which is also why JSON is widely used in program development. I have written a JavaScript function to convert XML into JSON ~ Duang ~ Duang ~ The pain point is that you want to die. If you want to convert an existing object to a JSON string, you can useJSON. stringify (obj)Function. You may have used this function (not supported in IE6 or IE7 ). However, you may not know that you can use Parameter Control to generate a beautiful JSON format during conversion!

In fact, it is very simple.JSON. stringifyThe third parameter of the function is used to specify the number of indented spaces:

// For example, an object is constructed literally. // in actual use, it is usually a POJO, or a value object such as VO var myObject = {"myProp ": "myValue", "subObj": {"prop": "value" }}; // format var formattedStr = JSON. stringify (myObject, null, 2 );

The generated string is as follows:

{"MyProp": "myValue", "subObj": {"prop": "value "}}

Original article: Indent JSON with JavaScript

Original Article Date: January 1, March 02, 2015

Translated on: February 1, March 09, 2015

Iron anchor http://blog.csdn.net/renfufei

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.