This is a creation in Article, where the information may have evolved or changed.
Design ideas:
1. Encounters the character ' {', ' [', tabnum++ \n\t*tabnum
2. Encounters the character ' ' ' must wait for the next character ' ' to appear, forming a string judgment.
3. Encounters the character ', ' \n\t*tabnum
4. Encounters the first character in front of the character '] ', '} ' must be inserted into the tabnum--\n\t*tabnum
The following characters do not have ', ' then directly tabnum--\n\t*tabnum
<pre name= "code" class= "plain" >func traversejsontotree (JSON []byte) []byte {stringflag: = Falsejsontree: = Make ([] BYTE, 0) Tabnum: = 0dirFlag: = Falsejsonlen: = Len (JSON) for I, Jschar: = Range JSON {if Stringflag {jsontree = append (jsont REE, Jschar) if Jschar = = ' "' {stringflag = false}continue}if Jschar = = '" ' {jsontree = append (Jsontree, Jschar) Stringflag = Truecontinue}if Jschar = = ' {' | | jschar = = ' [' {jsontree = append (Jsontree, jschar) jsontree = append (Jsontree, ' \ R ') JSON Tree = append (jsontree, ' \ n ') tabnum++for tabIndex: = 0; TabIndex < Tabnum; tabindex++ {jsontree = append (Jsontree, ' \ t ')}continue}if Jschar = = ', ' {jsontree = append (Jsontree, Jschar) Jsontree = App End (Jsontree, ' \ r ') Jsontree = append (jsontree, ' \ n ') for tabIndex: = 0; TabIndex < Tabnum; tabindex++ {jsontree = append (Jsontree, ' \ t ')}continue}if dirflag {dirflag = Falsetabnum--jsontree = Append (Jsontree, ' \ r ') Jsontree = append (jsontree, ' \ n ') for tabIndex: = 0; TabIndex < Tabnum; tabindex++ {jsontree = aPpend (Jsontree, ' \ t ')}jsontree = append (Jsontree, Jschar) continue}if Jschar = = '] ' | | Jschar = = '} ' {tabnum--jsontree = append (Jsontree, ' \ r ') Jsontree = append (jsontree, ' \ n ') for tabIndex: = 0; TabIndex < Tabnum; tabindex++ {jsontree = append (Jsontree, ' \ t ')}jsontree = append (Jsontree, Jschar) if (i + 1) < Jsonlen {if json[i+1]! = ', ' {dirflag = true}}continue}if Jschar = = ': ' | | (Jschar >= ' 0 ' && jschar <= ' 9 ') {jsontree = append (Jsontree, Jschar) Continue}} Return Jsontree}