Use Gojson in the GO program to parse JSON format files _golang

Source: Internet
Author: User
Tags json key string

Gojson is a Golang package that quickly parses JSON data, and you can use it to quickly find data in JSON
installation

 Go to Github.com/widuu/gojson

Use introduction

Structure

Copy Code code as follows:

Type Js struct {
Data interface{}
}

(1) Func JSON (data) *js data is a string type, initializes the Js structure, parses Json, and return Js.data
Copy Code code as follows:

JSON: = ' {' from ': ' en ', ' to ': ' En '} '
C1: = Gojson. JSON (JSON)//&{map[from:en To:zh]}

(2) func (*JS) get () *js gets a value in a simple JSON, recursive lookup, return js.data
Copy Code code as follows:

JSON: = ' {' from ': "en", "to": "en", "trans_result": {"src": "Today", "DST": "\u4eca\u5929"}, "result": [SRC], "Today", "DST "," \u4eca\u5929 "]} '

C2: = Gojson. JSON (JSON). Get ("Trans_result"). Get ("DST")
Fmt. Println (C2)//&{today}

C2: = Gojson. JSON (JSON). Get (' from ')
Fmt. Println (C2)//&{en}


(3) func (*JS) Tostring () string converts a single data to a string type because the string type is better than the other types. Let the data return string
Copy Code code as follows:

C2: = Gojson. JSON (JSON). Get (' from '). Tostring ()
Fmt. Println (C2)//en

(4) Func (J *js) GetPath (args ... string) *js get a value by entering multiple arguments of string, the JSON data must be recursive
Copy Code code as follows:

C4: = Gojson. JSON (JSON). GetPath ("Trans_result", "src"). Tostring ()
Fmt. Println (C4)//today

(5) Func (J *js) arrayindex (i int) string gets the value of the array structure in the JSON data, returns the corresponding value based on the input num, and is limited to handling {"result": [SRC], "Today", "DST", "\u4eca value in [] in \u5929″]}
Copy Code code as follows:

JSON: = ' {' from ': "en", "to": "en", "trans_result": {"src": "Today", "DST": "\u4eca\u5929"}, "result": [SRC], "Today", "DST "," \u4eca\u5929 "]} '
C7: = Gojson. JSON (JSON). Get (' result '). Arrayindex (1)
Fmt. PRINTLN (C7)//src

(6) Func (J *js) Getkey (key string, I int) *js This function is to have duplicate data in the data, take value, use js.data must be []interface{} type, this is the Baidu translation when the return of Js may use
Copy Code code as follows:

Json1: = ' {' from ': "en", "to": "en", "trans_result": [{"src": "Today", "DST": "\u4eca\u5929"},{"src": "Tomorrow", "DST": "\ u660e\u5929 "}]} '
C8: = Gojson. Json (Json1). Get ("Trans_result"). Getkey ("src", 1). Tostring ()
Fmt. PRINTLN (C8)/Then returns SRC in the first group of Trans_result today

(7) Func (J *js) ToArray () (k, d []string) converts the JSON data into the corresponding array of key []string{} value []string{} One by one, only to two levels and not to the multilevel
Copy Code code as follows:

c9k, c9v: = Gojson. Json (Json1). Get ("Trans_result"). ToArray ()
Fmt. Println (c9k, c9v)//[src DST SRC DST] [today tomorrow tomorrow]

C3K, c3v: = Gojson. JSON (JSON). GetIndex (1). ToArray ()
Fmt. Println (c3k, C3V)//[from] [en]


(8) Func (J *js) GetIndex (i int) *JS returns the data within JSON according to i, can be searched step by step
Copy Code code as follows:

Json1: = ' {' from ': "en", "to": "en", "trans_result": [{"src": "Today", "DST": "\u4eca\u5929"},{"src": "Tomorrow", "DST": "\ u660e\u5929 "}]} '

C10: = Gojson. Json (Json1). GetIndex (3). GetIndex (1). GetIndex (1). Get ("src"). Tostring ()
Fmt. Println (C10)//today


(9) Func (J *js) StringToArray () []string will {"Result": ["src", "Today", "DST", "\u4eca\u5929″]}" data in the data JSON, return to [] Slice of string
Copy Code code as follows:

C11: = Gojson. JSON (JSON). Get (' result '). StringToArray ()
Fmt. Println (C11)//[src today DST]

(a) Func (J *js) type () print test, print data type
Copy Code code as follows:

Gojson. JSON (JSON). Get (' result '). Type ()//[]interface {}

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.