json.NewDecoder
NewDecoder returns a new decoder that reads from r.
The decoder introduces its own buffering and may read data from r beyond the JSON values requested.
NewDecoder返回一个新的解码器,从r中读取数据。
解码器引入了它自己的缓冲区,并可能从r中读取超出请求的JSON值的数据。
json.NewDecoder(r io.Reader).Decode()
func (dec *Decoder) Decode(v interface{}) error
Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.
See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
Decode从其输入中读取下一个JSON编码的值,并将其存储在v所指向的值中。
关于将JSON转换为Go值的细节,请参见Unmarshal的文档。
json.Marshal
Marshal returns the JSON encoding of v.
Marshal traverses the value v recursively. If an encountered value implements the Marshaler interface and is not a nil pointer, Marshal calls its MarshalJSON method to produce JSON. If no MarshalJSON method is present but the value implements encoding.TextMarshaler instead, Marshal calls its MarshalText method and encodes the result as a JSON string. The nil pointer exception is not strictly necessary but mimics a similar, necessary exception in the behavior of UnmarshalJSON.
Marshal返回v的JSON编码。
Marshal递归地遍历值v。如果遇到的值实现了Marshaler接口并且不是一个nil指针,Marshal会调用它的MarshalJSON方法来产生JSON。如果没有MarshalJSON方法,但值实现了encoding.TextMarshaler,Marshal调用它的MarshalText方法,将结果编码为JSON字符串。nil指针异常不是严格意义上的必要条件,但它模仿了UnmarshalJSON行为中类似的、必要的异常。
json.MarshalIndent
MarshalIndent is like Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one or more copies of indent according to the indentation nesting.
MarshalIndent和Marshal一样,但应用缩进来格式化输出。输出中的每个JSON元素将在一个新的行上开始,以前缀开始,后面是根据缩进嵌套的一个或多个副本的缩进。
json.Unmarshal
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.
Unmarshal解析JSON编码的数据,并将结果存储在v所指向的值中。如果v是nil或不是一个指针,Unmarshal返回InvalidUnmarshalError。
- 可转成指针
- 可转成结构或slice
- 可转Unmarshaler接口的值中