网页 综合 更新时间 2026-11-14 16:29:20

json package - encoding/json/v2 - Go Packages

自动蜘蛛 go.dev 正文约 6000 字
Common problems companies solve with Go
Stories about how and why companies use Go
How Go can help keep you secure by default
Tips for writing clear, performant, and idiomatic Go code
A complete introduction to building software with Go
Reference documentation for Go's standard library
Learn what's new in each Go release
Reference documentation for the pkg.go.dev API
Videos from prior events
Meet other local Go developers
Learn and network with Go developers from around the world
The Go project's official blog.
Get help and stay informed from Go
This package is not in the latest version of its module.
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Modules with tagged versions give importers more predictable builds.
When a project reaches major version v1 it is considered stable.
Package json implements semantic processing of JSON as specified in RFC 8259. JSON is a simple data interchange format that can represent primitive data types such as booleans, strings, and numbers, in addition to structured data types such as objects and arrays.
See the Working with JSON tutorial for an introduction to this package.
Marshal and Unmarshal encode and decode Go values to/from JSON text contained within a []byte. MarshalWrite and UnmarshalRead operate on JSON text by writing to or reading from an io.Writer or io.Reader. MarshalEncode and UnmarshalDecode operate on JSON text by encoding to or decoding from a jsontext.Encoder or jsontext.Decoder. Options may be passed to each of the marshal or unmarshal functions to configure the semantic behavior of marshaling and unmarshaling (i.e., alter how JSON data is understood as Go data and vice versa). jsontext.Options may also be passed to the marshal or unmarshal functions to configure the syntactic behavior of encoding or decoding.
The data types of JSON are mapped to/from the data types of Go based on the closest logical equivalent between the two type systems. For example, a JSON boolean corresponds with a Go bool, a JSON string corresponds with a Go string, a JSON number corresponds with a Go int, uint or float, a JSON array corresponds with a Go slice or array, and a JSON object corresponds with a Go struct or map. See the documentation on Marshal and Unmarshal for a comprehensive list of how the JSON and Go type systems correspond.
Arbitrary Go types can customize their JSON representation by implementing Marshaler, MarshalerTo, Unmarshaler, or UnmarshalerFrom. This provides authors of Go types with control over how their types are serialized as JSON. Alternatively, users can implement functions that match MarshalFunc, MarshalToFunc, UnmarshalFunc, or UnmarshalFromFunc to specify the JSON representation for arbitrary types. This provides callers of JSON functionality with control over how any arbitrary type is serialized as JSON.
A Go struct is naturally represented as a JSON object, where each Go struct field corresponds with a JSON object member. When marshaling, all Go struct fields are recursively encoded in depth-first order as JSON object members except those that are ignored or omitted. When unmarshaling, JSON object members are recursively decoded into the corresponding Go struct fields. Object members that do not match any struct fields, also known as “unknown members”, are ignored by default or rejected if RejectUnknownMembers is specified.
The representation of each struct field can be customized in the "json" struct field tag, where the tag is a comma-separated list of options. As a special case, if the entire tag is `json:"-"`, then the field is ignored with regard to its JSON representation. Some options also have equivalent behavior controlled by a caller-specified Options. Field-specified options take precedence over caller-specified options.
The first option is the JSON object name override for the Go struct field. If the name is not specified, then the Go struct field name is used as the JSON object name. By default, unmarshaling uses case-sensitive matching to identify the Go struct field associated with a JSON object name.
After the name, the following tag options are supported:
omitzero: When marshaling, the "omitzero" option specifies that the struct field should be omitted if the field value is zero as determined by the "IsZero() bool" method if present, otherwise based on whether the field is the zero Go value. This option has no effect when unmarshaling.
omitempty: When marshaling, the "omitempty" option specifies that the struct field should be omitted if the field value would have been encoded as a JSON null, empty string, empty object, or empty array. This option has no effect when unmarshaling.
string: The "string" option specifies that StringifyNumbers be set when marshaling or unmarshaling a struct field value. This causes types that would normally be encoded as a JSON number to instead be encoded as a JSON number quoted within a JSON string, and to be decoded from a JSON string containing the JSON number without any surrounding whitespace. The "string" option only applies to the top-level of the Go struct field value. It is an error to apply this option to any type that does not encode as a JSON number. Note that composite types such as arrays, slices, structs, and maps do not encode as a JSON number, so applying this option will cause an error rather than affecting JSON numbers within such types. This extra level of encoding is often necessary since many JSON parsers cannot precisely represent 64-bit integers.
case: When unmarshaling, the "case" option specifies how JSON object names are matched with the JSON name for Go struct fields. The option is a key-value pair specified as "case:value" where the value must either be 'ignore' or 'strict'. The 'ignore' value specifies that matching is case-insensitive, and also ignores dashes and underscores. If mu
访问原文 返回搜索 https://go.dev/pkg/encoding/json/v2
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。