afox123 发表于 2018-9-19 14:12:24

Golang json用法详解(一)

  omitempty,tag里面加上omitempy,可以在序列化的时候忽略0值或者空值
  
```
  
package main
  import (
  
"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}
  
```


页: [1]
查看完整版本: Golang json用法详解(一)