标签:enc data print new erro [] pre lse hal
encoding/json
包package main
import (
"encoding/json"
"fmt"
)
type Movie struct {
Title string "name"
Year int "year"
Blue bool ""
Actors []string "actor array"
}
func JsonTest() {
m := new(Movie)
m.Title = "tom and jerry"
m.Year = 1992
m.Blue = false
m.Actors = []string{"tom", "jerry"}
fmt.Println(m)
// json.Marshal
data, err := json.Marshal(m)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(data)
fmt.Printf("%s\n", data)
// json.MarshalIndent
data, err = json.MarshalIndent(m, "", " ")
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(data)
fmt.Printf("%s\n", data)
// json.Unmarshal
um := new(Movie)
if err := json.Unmarshal(data, um); err != nil {
fmt.Println(err.Error())
}
fmt.Println(um)
}
func main() {
JsonTest()
}
标签:enc data print new erro [] pre lse hal
原文地址:https://www.cnblogs.com/walkinginthesun/p/10192768.html