A packet of Golang parsing JSON data written in Golang

Source: Internet
Author: User
Tags key string
This is a creation in Article, where the information may have evolved or changed.

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

Installation

 go get github.com/widuu/gojson

Introduction to use

Structure

type Js struct {    data interface{}}

(1) func Json(data) *Js data is a string type, initializes the JS structure, parses the JSON and returnJs.data

json := `{"from":"en","to":"zh"}`c1 := gojson.Json(json) //&{map[from:en to:zh]}

(2) func (*Js) Get() *js get a value from a simple JSON, recursively find, returnJs.data

json := `{"from":"en","to":"zh","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) //&{今天}c2 := gojson.Json(json).Get("from")fmt.Println(c2) //&{en}

(3) Convert func (*Js)Tostring()string a single data to a string type, because the string type is better when other types are improved let the data return a string

c2 := gojson.Json(json).Get("from").Tostring()fmt.Println(c2) //en

(4) func (j *Js) Getpath(args ...string) *Js by entering multiple parameters of string to get a value, the JSON data must be recursive

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 according to the input num, and is limited to processing {"Result": ["src", "Today", "DST", "\u4eca\u5929″]} [] values within []

json := `{"from":"en","to":"zh","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 for data to have duplicate data, value, use js.data must be []interface{} type, this is Baidu translation when the return JS may use

json1 := `{"from":"en","to":"zh","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) //则返回trans_result第一组中的src today

(7) func (j *Js) ToArray() (k, d []string) convert the JSON data to a key []string{} value []string{} one by one corresponding to the array, can only use to two levels can not go to the multilevel

c9k, c9v := gojson.Json(json1).Get("trans_result").ToArray()fmt.Println(c9k, c9v) //[src dst src dst] [today 今天 tomorrow 明天]c3k, c3v := gojson.Json(json).Getindex(1).ToArray()fmt.Println(c3k, c3v) //    [from] [en]

(8) func (j *Js) Getindex(i int) *Js According to I return data within the JSON, can be searched step by step

json1 := `{"from":"en","to":"zh","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 JSON of the result corresponding to the data, returned to []string Slice

c11 := gojson.Json(json).Get("result").StringtoArray()fmt.Println(c11) //[src today dst 今天]

func (j *Js) Type()print test, print data type

gojson.Json(json).Get("result").Type()  //[]interface {}

Without permission, do not reprint this site any article: Micro network» Golang written Golang parsing JSON data packets

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.