在Go語言程式中使用gojson來解析JSON格式檔案_Golang

來源:互聯網
上載者:User

gojson是快速解析json資料的一個golang包,你使用它可以快速的尋找json內的資料
安裝

 go get github.com/widuu/gojson

使用簡介

結構

複製代碼 代碼如下:

type Js struct {
    data interface{}
}

(1) func Json(data) *Js data為string類型,初始化Js結構,解析json並且return Js.data
複製代碼 代碼如下:

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

(2) func (*Js) Get() *js 擷取簡單json中的某個值,遞迴尋找,return Js.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) func (*Js)Tostring()string 將單個資料轉化成string類型,因為string類型轉其它類型都比較好轉就讓資料返回string
複製代碼 代碼如下:

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

(4) func (j *Js) Getpath(args ...string) *Js 通過輸入string的多個參數來擷取某個值,json資料一定要是遞迴的
複製代碼 代碼如下:

c4 := gojson.Json(json).Getpath("trans_result", "src").Tostring()
fmt.Println(c4)  //today

(5) func (j *Js) Arrayindex(i int) string 擷取Json資料中數組結構的值,根據輸入的num來返回對應的值,僅限於處理{“result”:[“src”,”today”,”dst”,”\u4eca\u5929″]}中[]內的值
複製代碼 代碼如下:

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 這個函數是針對資料中有重複資料,取值,使用js.data必須是[]interface{}類型,這個是百度翻譯時候返回的js可能會用到
複製代碼 代碼如下:

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)將json資料轉換成key []string{} value []string{} 一一對應的數組,只能使用到二級 不能到多級
複製代碼 代碼如下:

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 根據i返回json內的資料,可以逐級尋找
複製代碼 代碼如下:

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 將{“result”:[“src”,”today”,”dst”,”\u4eca\u5929″]}資料json中的result對應的資料,返回成[]string的slice
複製代碼 代碼如下:

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

(10) func (j *Js) Type() 列印測試用,列印資料類型
複製代碼 代碼如下:

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.