Documentation ¶
Overview ¶
Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions.
See "JSON and Go" for an introduction to this package: https://golang.org/doc/articles/json_and_go.html
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarshalIndent ¶
MarshalIndent is like json.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.
Example (Comments) ¶
var strct = struct { A string `c:"注释A"` B string `c:"注释B"` C string D string `c:"<注释D>"` }{ A: "1", B: "2", C: "3", D: "4", } b, err := MarshalIndent(strct, false, ``, ` `) fmt.Println(string(b), err)
Output: { "A": "1", # 注释A "B": "2", # 注释B "C": "3", "D": "4" # <注释D> } <nil>
Example (Empty_map) ¶
type node struct { Name string `c:"名称"` Children map[int]node `c:"孩子"` } b, err := MarshalIndent(node{}, false, ``, ` `) fmt.Println(string(b), err)
Output: { "Name": "", # 名称 "Children": { # 孩子 "0": { "Name": "", # 名称 "Children": null # 孩子 } } } <nil>
Example (Empty_slice) ¶
type node struct { Name string `c:"名称"` Children []node `c:"孩子"` } b, err := MarshalIndent(node{}, false, ``, ` `) fmt.Println(string(b), err)
Output: { "Name": "", # 名称 "Children": [ # 孩子 { "Name": "", # 名称 "Children": null # 孩子 } ] } <nil>
Example (Nil_pointer) ¶
type node struct { Name string `c:"名称"` Next *node `c:"后一个"` Time *time.Time } b, err := MarshalIndent(node{}, false, ``, ` `) fmt.Println(string(b), err)
Output: { "Name": "", # 名称 "*Next": { # 后一个 "Name": "", # 名称 "*Next": null, # 后一个 "*Time": "0001-01-01T00:00:00Z" }, "*Time": "0001-01-01T00:00:00Z" } <nil>
Example (Nil_pointer_to_anonymous_field) ¶
type node struct { Name string `c:"名称"` Next *node `c:"后一个"` Time *time.Time } b, err := MarshalIndent(struct{ *node }{}, false, ``, ` `) fmt.Println(string(b), err)
Output: { "Name": "", # 名称 "*Next": null, # 后一个 "*Time": "0001-01-01T00:00:00Z" } <nil>
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.