Javascript formatted json display instance analysis

Source: Internet
Author: User

Javascript formatted json display instance analysis

Javascript formatted json display.

The example analyzes the related techniques of json formatting in javascript, which is very useful. For more information, see

This example describes how to format json in javascript. Share it with you for your reference. The specific analysis is as follows:

Formatting json objects or json strings is convenient on the webpage

?

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

43

44

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,

PADDING = '';

// One can also use '\ t' or a different number of spaces

// Optional settings

Options = options | {};

// Remove newline where '{' or '['follows ':'

Options. newlineAfterColonIfBeforeBraceOrBracket = (options. newlineAfterColonIfBeforeBraceOrBracket = true )? True: false;

// Use a space 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. stringify (json );

} Else {

// Is already a string, so parse and re-stringify

// In 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, '\ r \ n $1 \ r \ n ');

// Add newline before and after square brackets

Reg =/([\ [\])/g;

Json = json. replace (reg, '\ r \ n $1 \ r \ n ');

// Add newline after comma

Reg =/(\,)/g;

Json = json. replace (reg, '$1 \ r \ n ');

// Remove multiple newlines

Reg =/(\ r \ n)/g;

Json = json. replace (reg, '\ r \ n ');

// Remove newlines before commas

Reg =/\ r \ n \,/g;

Json = json. replace (reg ,',');

// Optional formatting...

If (! Options. newlineAfterColonIfBeforeBraceOrBracket ){

Reg =/\: \ r \ n \ {/g;

Json = json. replace (reg ,':{');

Reg =/\: \ r \ n \ [/g;

Json = json. replace (reg ,':[');

}

If (options. spaceAfterColon ){

Reg =/\:/g;

Json = json. replace (reg ,':');

}

$. Each (json. split ('\ r \ n'), 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 + '\ r \ n ';

Pad + = indent;

});

Return formatted;

};

I hope this article will help you design javascript programs.

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.