26、golang中的json库
package main
import (
"encoding/json"
"fmt"
)
type Order struct {
ID string `json:"id"`
//Name string `json:"name,omitempty"`//omitempty表示当name字段没值时,省略该字段
Items []*OrderItem
Quantity int `json:"quantity"`
TotalPrice int `json:"total_price"`
}
type OrderItem struct {
ID string `json:"id"`
Name string `json:"name"`
Price float64 `json:"price"`
}
func main() {
o := Order{
ID: "3",
//Name: "study golang",
Items: []*OrderItem{
{
ID: "3",
Name: "鞋子",
Price: 30.3,
},
{
ID: "4",
Name: "袜子",
Price: 3.3,
},
},
Quantity: 3,
TotalPrice: 30,
}
order, err := json.Marshal(o)
if err != nil {
panic(err)
}
fmt.Printf("%s", order)
}