Marshal
生成Json
package main
import (
"encoding/json"
"fmt"
)
type Order struct {
ID string
Name string
Quantity int
TotalPrice float64
}
func main() {
o := Order{
ID: "123456",
Name: "吹风机",
Quantity: 1,
TotalPrice: 45,
}
bytes, err := json.Marshal(o)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
{
"ID": "123456",
"Name": "吹风机",
"Quantity": 1,
"TotalPrice": 45
}
以上输出的Json就可以再网络上进行传输,绝大部分语言都对Json有良好的支持.但是上面的Json所有的字段名都是首字母大写的,再其他语言上是不多见的,我们希望遵循网络上常用的大小写的使用方式
修改首字母大写
package main
import (
"encoding/json"
"fmt"
)
type Order struct {
id string
name string
quantity int
totalPrice float64
}
func main() {
o := Order{
id: "123456",
name: "吹风机",
quantity: 1,
totalPrice: 45,
}
bytes, err := json.Marshal(o)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
将字段名改成小写以后,生成的Json是{};主要原因是因为再go语言中,首字母大写的都是public,首字母小写的都是private;我们将首字母改成小写后就全部变成了private;再Marshal时是读不到的
tag
package main
import (
"encoding/json"
"fmt"
)
type Order struct {
ID string `json:"id"`
Name string `json:"name"`
Quantity int `json:"quantity"`
TotalPrice float64 `json:"total_price"`
}
func main() {
o := Order{
ID: "123456",
Name: "吹风机",
Quantity: 1,
TotalPrice: 45,
}
bytes, err := json.Marshal(o)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
{
"id": "123456",
"name": "吹风机",
"quantity": 1,
"total_price": 45
}
我们再每个属性后面都加上了tag,tag其实是没有什么意义的,它的整个go语言的运行中没有什么意义,只是给json.Marshal看的;如果有tag再生成json的时候会按照tag里面的内容替换成大写的属性名.
空串的处理
不过滤空串
package main
import (
"encoding/json"
"fmt"
)
type Order struct {
ID string `json:"id"`
Name string `json:"name"`
Quantity int `json:"quantity"`
TotalPrice float64 `json:"total_price"`
}
func main() {
o := Order{
ID: "123456",
//Name: "吹风机",
Quantity: 1,
TotalPrice: 45,
}
bytes, err := json.Marshal(o)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
{
"id": "123456",
"name": "",
"quantity": 1,
"total_price": 45
}
我们不对Name进行定义时,生成Json时会生成一个空串,json.Marshal是可以对空串进行过滤的
过滤空串
package main
import (
"encoding/json"
"fmt"
)
type Order struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Quantity int `json:"quantity"`
TotalPrice float64 `json:"total_price"`
}
func main() {
o := Order{
ID: "123456",
//Name: "吹风机",
Quantity: 1,
TotalPrice: 45,
}
bytes, err := json.Marshal(o)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
{
"id": "123456",
"quantity": 1,
"total_price": 45
}
我们再tag里面加入omitempty再Marshal时是可以对空串进行过滤的(是否过滤根据实际的应用场景来)
复杂Json的处理
package main
import (
"encoding/json"
"fmt"
)
type OrderItem struct {
ID string `json:"id"`
Name string `json:"name"`
Price float64 `json:"price"`
}
type UserCoupon struct {
ID string `json:"id"`
Title string `json:"title"`
Used string `json:"used"`
}
type UserGrade struct {
ID string `json:"id"`
GradeNum int `json:"grade_num"`
}
type Order struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Quantity int `json:"quantity"`
TotalPrice float64 `json:"total_price"`
Items []OrderItem `json:"items"`
Grade *UserGrade `json:"grade"`
Coupon UserCoupon `json:"coupon"`
}
func main() {
o := Order{
ID: "123456",
Quantity: 1,
TotalPrice: 65,
Items: []OrderItem{
{ID: "123", Name: "吹风机", Price: 45},
{ID: "124", Name: "台灯", Price: 35},
},
Grade: &UserGrade{
ID: "321", GradeNum: 1,
},
Coupon: UserCoupon{
ID: "126",
Title: "满50减15",
Used: "店铺优惠券",
},
}
bytes, err := json.Marshal(o)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
{
"id": "123456",
"quantity": 1,
"total_price": 65,
"items": [
{
"id": "123",
"name": "吹风机",
"price": 45
},
{
"id": "124",
"name": "台灯",
"price": 35
}
],
"grade": {
"id": "321",
"grade_num": 1
},
"coupon": {
"id": "126",
"title": "满50减15",
"used": "店铺优惠券"
}
}
json.Marshal支持对复杂Json的处理.包括但不限于数组,Json的嵌套,指针的使用.
Unmarshal
package main
import (
"encoding/json"
"fmt"
)
type OrderItem struct {
ID string `json:"id"`
Name string `json:"name"`
Price float64 `json:"price"`
}
type UserCoupon struct {
ID string `json:"id"`
Title string `json:"title"`
Used string `json:"used"`
}
type UserGrade struct {
ID string `json:"id"`
GradeNum int `json:"grade_num"`
}
type Order struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Quantity int `json:"quantity"`
TotalPrice float64 `json:"total_price"`
Items []OrderItem `json:"items"`
Grade *UserGrade `json:"grade"`
Coupon UserCoupon `json:"coupon"`
}
func main() {
s := `{"id":"123456","quantity":1,"total_price":65,"items":[{"id":"123","name":"吹风机","price":45},{"id":"124","name":"台灯","price":35}],"grade":{"id":"321","grade_num":1},"coupon":{"id":"126","title":"满50减15","used":"店铺优惠券"}}`
var o Order
err := json.Unmarshal([]byte(s), &o)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n",o)
}
将Json转换成struct用json.Unmarshal就可以了,go语言对解析Json有同样良好的支持,再解析的时候要注意的是一定要把struct的地址传过去,如果将值传过去,那么就是数据的复制,不会影响到自己定义的struct;只有将地址传过去才能完成 正常的解析