编码
json.Marshal()
func Marshal(v interface{}) ([]byte, error)
解码
json.Unmarshal()
func Unmarshal(data []byte, v interface{}) error
json TO struct在线转译 :http://json2struct.mervine.net/
json to go滴滴团队研发(据说比官方快6倍):https://github.com/json-iterator/go
Warning:Json转结构体,或结构体转Json,结构体首字母必须大写!!!
package main
import (
"fmt"
"encoding/json"
)
func main() {
type Student struct {
Name string
Age int
Classes []string
Prices float64
}
st := &Student {
"xiaoming",
18,
[]string{"math","english","chinese"},
9.99,
}
result, err := json.Marshal(st)
if(err == nil) {
fmt.Println(string(result))
}
}
//{"Name":"xiaoming","Age":18,"Classes":["math","english","chinese"],"Prices":9.99}
有疑问加站长微信联系(非本文作者)