Golang json用法詳解(一)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Golang json用法詳解(一)

簡介

json格式可以算我們日常最常用的序列化格式之一了,Go語言作為一個由Google開發,號稱互連網的C語言的語言,自然也對JSON格式支援很好。但是Go語言是個強型別語言,對格式要求極其嚴格而JSON格式雖然也有類型,但是並不穩定,Go語言在解析來源為非強型別語言時比如PHP等序列化的JSON時,經常遇到一些問題諸如欄位類型變化導致無法正常解析的情況,導致服務不穩定。所以本篇的主要目的

  1. 就是挖掘Golang解析json的絕大部分能力
  2. 比較優雅的解決解析json時存在的各種問題
  3. 深入一下Golang解析json的過程

Golang解析JSON之Tag篇

  1. 一個結構體正常序列化過後是什麼樣的呢?

    package mainimport (    "encoding/json"    "fmt")// Product 商品資訊type Product struct {    Name      string    ProductID int64    Number    int    Price     float64    IsOnSale  bool}func main() {    p := &Product{}    p.Name = "Xiao mi 6"    p.IsOnSale = true    p.Number = 10000    p.Price = 2499.00    p.ProductID = 1    data, _ := json.Marshal(p)    fmt.Println(string(data))}//結果{"Name":"Xiao mi 6","ProductID":1,"Number":10000,"Price":2499,"IsOnSale":true}
  2. 何為Tag,tag就是標籤,給結構體的每個欄位打上一個標籤,標籤冒號前是類型,後面是標籤名。

    // Product _type Product struct {    Name      string  `json:"name"`    ProductID int64   `json:"-"` // 表示不進行序列化    Number    int     `json:"number"`    Price     float64 `json:"price"`    IsOnSale  bool    `json:"is_on_sale,string"`}// 序列化過後,可以看見   {"name":"Xiao mi 6","number":10000,"price":2499,"is_on_sale":"false"}
  3. omitempty,tag裡面加上omitempy,可以在序列化的時候忽略0值或者空值

    package mainimport (    "encoding/json"    "fmt")// Product _type Product struct {    Name      string  `json:"name"`    ProductID int64   `json:"product_id,omitempty"`     Number    int     `json:"number"`    Price     float64 `json:"price"`    IsOnSale  bool    `json:"is_on_sale,omitempty"`}func main() {    p := &Product{}    p.Name = "Xiao mi 6"    p.IsOnSale = false    p.Number = 10000    p.Price = 2499.00    p.ProductID = 0    data, _ := json.Marshal(p)    fmt.Println(string(data))}// 結果{"name":"Xiao mi 6","number":10000,"price":2499}
  4. type,有些時候,我們在序列化或者還原序列化的時候,可能結構體類型和需要的類型不一致,這個時候可以指定,支援string,number和boolean

    package mainimport (    "encoding/json"    "fmt")// Product _type Product struct {    Name      string  `json:"name"`    ProductID int64   `json:"product_id,string"`    Number    int     `json:"number,string"`    Price     float64 `json:"price,string"`    IsOnSale  bool    `json:"is_on_sale,string"`}func main() {    var data = `{"name":"Xiao mi 6","product_id":"10","number":"10000","price":"2499","is_on_sale":"true"}`    p := &Product{}    err := json.Unmarshal([]byte(data), p)    fmt.Println(err)    fmt.Println(*p)}// 結果<nil>{Xiao mi 6 10 10000 2499 true}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.