JavaScript formatted JSON display instance analysis

Source: Internet
Author: User
Tags json

javascript formats JSON display .

Examples of JavaScript operation JSON format of the relevant skills, very practical value, the need for friends can refer to the

This example describes the JavaScript formatted JSON display method. Share to everyone for your reference. The specific analysis is as follows:

Format a JSON object or JSON string to easily restrict on a Web page

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68-69 var Formatjson = function (JSON, options) {var reg = null, formatted = ', pad = 0, pad DING = '; One can also use ' t ' or a different number of spaces//Optional settings options = Options | | {}; Remove newline where ' {' or ' [' follows ': ' Options.newlineaftercolonifbeforebraceorbracket = (options.newlineaftercol Onifbeforebraceorbracket = = True)? True:false; Use a spaces after a colon Options.spaceaftercolon = (Options.spaceaftercolon = = False)? False:true; Begin formatting ... if (typeof json!== ' string ') {//Make sure we start with the JSON as a string json = Json.stringi FY (JSON); else {//is already a string, so parse and re-stringify//in the order to remove extra whitespace json = Json.parse (JSON); JSON = json.stringify (JSON); }//Add newline before and after curly braces Reg =/([{}])/g; JSON = Json.replace (Reg, ' rn$1rn '); Add newline before and after square brackets reg =/([[]])/g; JSON = Json.replace (Reg, ' rn$1rn '); //Add newline after comma Reg =/(,)/g; JSON = Json.replace (Reg, ' $1rn '); Remove multiple Newlines reg =/(rnrn)/g; JSON = Json.replace (Reg, ' RN '); Remove newlines before commas reg =/rn,/g; JSON = Json.replace (Reg, ', '); Optional formatting. if (!options.newlineaftercolonifbeforebraceorbracket) {reg =/:rn{/g; json = Json.replace (Reg, ':{'); Reg =/:rn[/g json = json.replace (Reg, ': [');} if (Options.spaceaftercolon) {reg =/:/g; json = Json.replace (Reg, ': ') ; } $.each (Json.split (' rn '), function (Index, node) {var i = 0, indent = 0, padding = '; if (Node.match (/{$/) | | node.match (/[$/)) {indent = 1;} else if (Node.match (/}/) | | Node.match (/]/)) {if (Pad!== 0) {pad = 1;}} else {indent = 0;} for (i = 0; i < pad; i++) {padding = padding; } formatted + = padding + node + ' RN '; Pad + + indent; }); return formatted; };

The

wants this article to help you with your JavaScript programming.

Related Article

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.