Documentation
¶
Overview ¶
Example (BindToStruct) ¶
package main import ( "fmt" "github.com/winterssy/gjson" ) func main() { const dummyData = ` { "code": 200, "data": { "list": [ { "artist": "周杰伦", "album": "周杰伦的床边故事", "name": "告白气球" }, { "artist": "周杰伦", "album": "说好不哭 (with 五月天阿信)", "name": "说好不哭 (with 五月天阿信)" } ] } } ` var s struct { Code int `json:"code"` Data struct { List gjson.Array `json:"list"` } `json:"data"` } err := gjson.UnmarshalFromString(dummyData, &s) if err != nil { panic(err) } fmt.Println(s.Data.List.Index(0).ToObject().GetString("name")) }
Output: 告白气球
Example (ParseToObject) ¶
package main import ( "fmt" "io/ioutil" "log" "github.com/winterssy/gjson" ) func main() { data, err := ioutil.ReadFile("./testdata/music.json") if err != nil { log.Fatal(err) } obj, err := gjson.Parse(data) if err != nil { panic(err) } fmt.Println(obj.GetNumber("code")) fmt.Println(obj.GetString("data", "total")) fmt.Println(obj.GetArray("data", "list").Index(0).ToObject().GetString("name")) }
Output: 200 1917 告白气球
Index ¶
- Variables
- func Decode(data []byte, v interface{}, opts ...func(dec *Decoder)) error
- func DecodeFromString(s string, v interface{}, opts ...func(dec *Decoder)) error
- func Encode(v interface{}, opts ...func(enc *Encoder)) ([]byte, error)
- func EncodeToString(v interface{}, opts ...func(enc *Encoder)) (string, error)
- func MarshalIndentToString(v interface{}, prefix, indent string) (string, error)
- func MarshalToString(v interface{}) (string, error)
- func UnmarshalFromString(s string, v interface{}) error
- type Any
- func (v *Any) Interface() interface{}
- func (v *Any) IsNull() bool
- func (v *Any) MarshalJSON() ([]byte, error)
- func (v *Any) ToArray() Array
- func (v *Any) ToBoolean() bool
- func (v *Any) ToNumber() Number
- func (v *Any) ToObject() Object
- func (v *Any) ToString() string
- func (v *Any) UnmarshalJSON(data []byte) error
- type Array
- type Decoder
- type Encoder
- type Number
- func (n Number) ToFloat32() float32
- func (n Number) ToFloat64() float64
- func (n Number) ToInt() int
- func (n Number) ToInt16() int16
- func (n Number) ToInt32() int32
- func (n Number) ToInt64() int64
- func (n Number) ToInt8() int8
- func (n Number) ToString() string
- func (n Number) ToUint() uint
- func (n Number) ToUint16() uint16
- func (n Number) ToUint32() uint32
- func (n Number) ToUint64() uint64
- func (n Number) ToUint8() uint8
- type Object
- func (obj Object) GetAny(key string, childNodes ...string) *Any
- func (obj Object) GetArray(key string, childNodes ...string) Array
- func (obj Object) GetBoolean(key string, childNodes ...string) bool
- func (obj Object) GetNumber(key string, childNodes ...string) Number
- func (obj Object) GetObject(key string, childNodes ...string) Object
- func (obj Object) GetString(key string, childNodes ...string) string
- func (obj Object) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( Marshal = json.Marshal MarshalIndent = json.MarshalIndent Unmarshal = json.Unmarshal )
Methods from encoding/json or jsoniter.
Functions ¶
func DecodeFromString ¶
DecodeFromString is like Decode, but it reads from string instead of []byte.
func EncodeToString ¶
EncodeToString is like Encode, but it returns the string instead of []byte.
func MarshalIndentToString ¶
MarshalIndentToString is like MarshalIndent, but it returns the string instead of []byte.
func MarshalToString ¶
MarshalToString is like Marshal, but it returns the string instead of []byte.
func UnmarshalFromString ¶
UnmarshalFromString is like Unmarshal, but it reads from string instead of []byte.
Types ¶
type Any ¶
type Any struct {
// contains filtered or unexported fields
}
Any is a wrapper around an interface{}, represents any JSON value.
func (*Any) Interface ¶
func (v *Any) Interface() interface{}
Interface returns the original data of v or nil if v is nil.
func (*Any) IsNull ¶
IsNull reports whether the original data of v is null or not. Note: if v is nil, it returns false.
func (*Any) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface.
func (*Any) ToArray ¶
ToArray casts v to an Array. If v is nil or the data type doesn't match, it will return the zero value.
func (*Any) ToBoolean ¶
ToBoolean casts v to a bool. If v is nil or the data type doesn't match, it will return the zero value.
func (*Any) ToNumber ¶
ToNumber casts v to a Number. If v is nil or the data type doesn't match, it will return the zero value.
func (*Any) ToObject ¶
ToObject casts v to an Object. If v is nil or the data type doesn't match, it will return the zero value.
func (*Any) ToString ¶
ToString casts v to a string. If v is nil or the data type doesn't match, it will return the zero value.
func (*Any) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface.
type Array ¶
type Array []*Any
Array is a shortcut for []*Any, represents JSON arrays.
func (Array) Index ¶
Index provides an elegant way to get arr's i'th element without panic even index out of range.
func (Array) ToBooleans ¶
ToBooleans casts arr to a bool array.
type Object ¶
type Object map[string]interface{}
Object is a shortcut for map[string]interface{}, represents JSON objects.
func ParseFromReader ¶
ParseFromReader reads the next JSON-encoded value from its input and stores it in an Object.
func ParseFromString ¶
ParseFromString is like Parse, but it reads from string instead of []byte.
func (Object) GetAny ¶
GetAny gets any value associated with key recursively. If the key not exists, it returns nil.
func (Object) GetArray ¶
GetArray gets array value associated with key recursively. If the key not exists, it returns the zero value.
func (Object) GetBoolean ¶
GetBoolean gets boolean value associated with key recursively. If the key not exists, it returns the zero value.
func (Object) GetNumber ¶
GetNumber gets number value associated with key recursively. If the key not exists, it returns the zero value.
func (Object) GetObject ¶
GetObject gets object value associated with key recursively. If the key not exists, it returns the zero value.