zoukankan      html  css  js  c++  java
  • Golang中 json tag 标签的作用和用法讲解

    结构体的tag

    tag是结构体的元信息,运行时通过反射机制读取。结构体的tag一般定义在相应字段的后面,格式为:

    fieldName fieldType  `key1:"value1" key2:"value2"`

    同一个结构体字段可以设置多个键值对tag,不同的键值对之间使用空格分隔。

    json tag

    默认情况下序列化与反序列化使用的都是结构体的原生字段名,可以通过给结构体字段添加json tag来指定序列化后的字段名。标签冒号前是类型,后面是标签名。
    例如代码:

     1 // Product _
     2 type Product struct {
     3     Name      string  `json:"name"`
     4     ProductID int64   `json:"-"` // 表示不进行序列化
     5     Number    int     `json:"number"`
     6     Price     float64 `json:"price"`
     7     IsOnSale  bool    `json:"is_on_sale,string"`
     8 }
     9  
    10 // 序列化过后,可以看见
    11    {"name":"Xiao mi 6","number":10000,"price":2499,"is_on_sale":"false"}

    omitempty,tag里面加上omitempy,可以在序列化的时候忽略0值或者空值。注意此时在“omitempty”前一定指定一个字段名,否则“omitempty”将作为字段名处理。

     1 package main
     2  
     3 import (
     4     "encoding/json"
     5     "fmt"
     6 )
     7  
     8 // Product _
     9 type Product struct {
    10     Name      string  `json:"name"`
    11     ProductID int64   `json:"product_id,omitempty"`
    12     Number    int     `json:"number"`
    13     Price     float64 `json:"price"`
    14     IsOnSale  bool    `json:"is_on_sale,omitempty"`
    15 }
    16  
    17 func main() {
    18     p := &Product{}
    19     p.Name = "Xiao mi 6"
    20     p.IsOnSale = false
    21     p.Number = 10000
    22     p.Price = 2499.00
    23     p.ProductID = 0
    24  
    25     data, _ := json.Marshal(p)
    26     fmt.Println(string(data))
    27 }
    28 // 结果(省略掉了p.IsOnSale 和 p.ProductID)
    29 {"name":"Xiao mi 6","number":10000,"price":2499}

    若要在被嵌套结构体整体为空时使其在序列化结果中被忽略,不仅要在被嵌套结构体字段后加上json:"fileName,omitempty",还要将其改为结构体指针。如:

     1 package main
     2 
     3 import (
     4     "encoding/json"
     5     "fmt"
     6 )
     7 
     8 type BodyInfo struct {
     9     Weight float64
    10     Height float64
    11 }
    12 
    13 type Student struct {
    14     Name      string `json:"name"`
    15     Age       int64
    16     *BodyInfo `json:"bodyinfo,omitempty"`
    17 }
    18 
    19 func main() {
    20     s1 := Student{
    21         Name: "jack",
    22         Age:  20,
    23     }
    24 
    25     data, _ := json.Marshal(s1)
    26     fmt.Println(string(data))
    27 }
    28 
    29 //结果
    30 {"name":"jack","Age":20}

    `json:",inline"`通常作用于内嵌的结构体类型,具体用法看下面这个例子:

     1 package main
     2 
     3 import (
     4     "encoding/json"
     5     "fmt"
     6 )
     7 
     8 type Project struct {
     9     Key   string `json:"key"`
    10     Value string `json:"value"`
    11 }
    12 
    13 type JiraHttpReqField struct {
    14     Project     `json:"project"`   // `json:",inline"`
    15     Summary     string `json:"summary"`
    16     Description string `json:"description"`
    17 }
    18 
    19 func main() {
    20     dataProject := Project{
    21         Key:   "name",
    22         Value: "zhangsan",
    23     }
    24     dataJiraHttpReqField := &JiraHttpReqField{
    25         Project:     dataProject,
    26         Summary:     "my summary",
    27         Description: "my description",
    28     }
    29     data, _ := json.Marshal(dataJiraHttpReqField)
    30     fmt.Println(string(data))
    31 }
    32 
    33 14行为`json:"project"`时的输出结果:
    34 {"project":{"key":"name","value":"zhangsan"},"summary":"my summary","description":"my description"}
    35 
    36 14行为`json:",inline"`时的输出结果:
    37 {"key":"name","value":"zhangsan","summary":"my summary","description":"my description"}

    type,有些时候,我们在序列化或者反序列化的时候,可能结构体类型和需要的类型不一致,这个时候可以指定,支持string,number和boolean

     1 package main
     2  
     3 import (
     4     "encoding/json"
     5     "fmt"
     6 )
     7  
     8 // Product _
     9 type Product struct {
    10     Name      string  `json:"name"`
    11     ProductID int64   `json:"product_id,string"`
    12     Number    int     `json:"number,string"`
    13     Price     float64 `json:"price,string"`
    14     IsOnSale  bool    `json:"is_on_sale,string"`
    15 }
    16  
    17 func main() {
    18  
    19     var data = `{"name":"Xiao mi 6","product_id":"10","number":"10000","price":"2499","is_on_sale":"true"}`
    20     p := &Product{}
    21     err := json.Unmarshal([]byte(data), p)
    22     fmt.Println(err)
    23     fmt.Println(*p)
    24 }
    25 // 结果
    26 <nil>
    27 {Xiao mi 6 10 10000 2499 true}

    参考文章:

    GO--Json tag标签的作用,json用法讲解:https://blog.csdn.net/qq_33679504/article/details/100533703

    golang-json使用(json tag):https://blog.csdn.net/somanlee/article/details/106925278

  • 相关阅读:
    000IntelliJ IDEA字体大小设置 超级菜鸟
    Zend_Captcha_Image使用及部分功能重写 基于zend Framework 1.6
    人民币存贷款基准利率调整表20110707
    PHP 取得某天前后N天的日期
    jquery 实现 鼠标拖拽元素复制并写入效果
    使用 JScript 创建 .exe 或 .dll 文件
    js window.location
    jquery 中的 $("#air") 与 document.getElementById("air") 的区别
    jquery 查找 iframe 父级页面元素
    php 批量替换文件名
  • 原文地址:https://www.cnblogs.com/FengZeng666/p/14987939.html
Copyright © 2011-2022 走看看