// NewEncoder returns a new encoder that writes to w.funcNewEncoder(w io.Writer)*Encoder
//Encode writes the JSON encoding of v to the stream.func(enc *Encoder)Encode(v interface{})error// 使用方法// 结构体转为[]byte
m :=struct{
Name, Text string}{
Name:"Ed", Text:"Knock knock.",}var s bytes.Buffer
err := json.NewEncoder(&s).Encode(m)
str := s.Bytes()
type Server struct{// ID 不会导出到JSON中
ID int`json:"-"`
ServerName string`json:"serverName"`// code在json中的类型会变成string
code int`json:"code,string"`// 如果 ServerIP 为空,则不输出到JSON串中
ServerIP string`json:"serverIP,omitempty"`}