Golang converting a JSON string to a tree structure

Source: Internet
Author: User
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}


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.