Documentation ¶
Overview ¶
Example (Usage) ¶
jsonstr := `{ "timestamp": "1438194274", "users": [{"id": 1, "name": "Tom"}, {"id": 2, "name": "Peter"}], "keywords": ["golang", "json"] }` j, _ := Unmarshal([]byte(jsonstr)) // Output: Tom fmt.Println(j.GetString("users", 0, "name")) // Output: [golang json] fmt.Println(j.Get("keywords").StringArray()) // Output: [Tom Peter] names, _ := j.Get("users").EachOf("name").StringArray() fmt.Println(names) // try parse STRING as NUMBER j.StringAsNumber() // Output: 1438194274 fmt.Println(j.Get("timestamp").Int()) // convert NUMBER, STRING, ARRAY and OBJECT type to BOOL j.AllAsBool() // Output: false fmt.Println(j.GetBool("status")) // using Unmarshal with path which can speed up json decode j, _ = Unmarshal([]byte(jsonstr), "users", 1, "name") fmt.Println(j.String())
Output: Tom <nil> [golang json] <nil> [Tom Peter] 1438194274 <nil> false <nil> Peter <nil>
Index ¶
- Constants
- Variables
- type Json
- func (j *Json) AllAsBool()
- func (j Json) Array() ([]Json, error)
- func (j Json) Bool() (bool, error)
- func (j Json) BoolArray() ([]bool, error)
- func (j Json) EachOf(keys ...interface{}) Json
- func (j Json) Element(i int) Json
- func (j Json) Error() error
- func (j Json) Float() (float64, error)
- func (j Json) FloatArray() ([]float64, error)
- func (j Json) Get(keys ...interface{}) Json
- func (j Json) GetBool(keys ...interface{}) (bool, error)
- func (j Json) GetFloat(keys ...interface{}) (float64, error)
- func (j Json) GetInt(keys ...interface{}) (int64, error)
- func (j Json) GetString(keys ...interface{}) (string, error)
- func (j Json) Int() (int64, error)
- func (j Json) IntArray() ([]int64, error)
- func (j Json) IsArray() bool
- func (j Json) IsBool() bool
- func (j Json) IsNull() bool
- func (j Json) IsNumber() bool
- func (j Json) IsObject() bool
- func (j Json) IsString() bool
- func (j Json) Keys() ([]string, error)
- func (j Json) Len() (int, error)
- func (j Json) Member(name string) Json
- func (j Json) String() (string, error)
- func (j Json) StringArray() ([]string, error)
- func (j *Json) StringAsNumber()
- func (j Json) Type() Type
- func (j Json) Values() ([]Json, error)
- type Number
- type Type
Examples ¶
Constants ¶
const (
ParseMemberNamesOnly = "__member_names_only__"
)
Variables ¶
var (
ErrMoreBytes = errors.New("not all bytes unmarshaled")
)
Functions ¶
This section is empty.
Types ¶
type Json ¶
type Json struct {
// contains filtered or unexported fields
}
Json represents everything of json
func DecodeFrom ¶
DecodeFrom parses data from reader to json
func Unmarshal ¶
Unmarshal parses data to Json. When specified keys, jsonport skips unused field for performance
func (*Json) AllAsBool ¶
func (j *Json) AllAsBool()
AllAsBool enables conversion of Bool():
STRING, ARRAY, OBJECT: true if Len() != 0 NUMBER: true if Float() != 0 NULL: false
Example ¶
jsonstr := `{"enabled": 1}` j, _ := Unmarshal([]byte(jsonstr)) fmt.Println("Without AllAsBool():") b, err := j.GetBool("enabled") fmt.Println(b, err) fmt.Println("With AllAsBool():") j.AllAsBool() b, err = j.GetBool("enabled") fmt.Println(b, err)
Output: Without AllAsBool(): false type mismatch: expected BOOL, found NUMBER With AllAsBool(): true <nil>
func (Json) Array ¶
Array converts current json value to []Json. error is returned if value type not equal to ARRAY.
func (Json) BoolArray ¶
BoolArray converts current json value to []bool. error is returned if any element type not equal to NUMBER.
func (Json) EachOf ¶
EachOf convert every elements specified by keys in json value to ARRAY. Json.Error() is set if an error occurred. it is equal to `Json{[e.Get(keys...) for e in j.Array()]}`
func (Json) Element ¶
Element returns the (i+1)th element of array. a NULL type Json is returned if index out of range. Json.Error() is set if type not equal to ARRAY.
func (Json) FloatArray ¶
FloatArray converts current json value to []float64. error is returned if any element type not equal to NUMBER.
func (Json) Get ¶
Get returns Json object by key sequence.
key with the type of string is equal to j.Member(k), key with the type of number is equal to j.Element(k), j.Get("key", 1) is equal to j.Member("key").Element(1).
a NULL type Json returned with err if:
- key type not supported. (neither number nor string)
- json value type mismatch.
func (Json) GetBool ¶
GetBool convert json value specified by keys to bool, it is equal to Get(keys...).Bool()
func (Json) GetFloat ¶
GetFloat convert json value specified by keys to float64, it is equal to Get(keys...).Float()
func (Json) GetInt ¶
GetInt convert json value specified by keys to int64, it is equal to Get(keys...).Int()
func (Json) GetString ¶
GetString convert json value specified by keys to string, it is equal to Get(keys...).String()
func (Json) Int ¶
Int converts current json value to int64 result will be negative if pasring from uint64 like `1<<63`
func (Json) IntArray ¶
IntArray converts current json value to []int64. error is returned if any element type not equal to NUMBER.
func (Json) Keys ¶
Keys returns the field names of json object. error is returned if value type not equal to OBJECT.
func (Json) Len ¶
Len returns the length of json value.
STRING: the number of bytes ARRAY: the number of elements OBJECT: the number of pairs
func (Json) Member ¶
Member returns the member value specified by `name` a NULL type Json is returned if member not found Json.Error() is set if type not equal to OBJECT
func (Json) StringArray ¶
StringArray converts current json value to []string. error is returned if any element type not equal to STRING.
func (*Json) StringAsNumber ¶
func (j *Json) StringAsNumber()
StringAsNumber enables conversion like {"id": "123"}, j.GetInt("id") is returned 123 instead of an err, j.GetString("id") is returned "123" as expected.
Example ¶
jsonstr := `{"timestamp": "1438194274"}` j, _ := Unmarshal([]byte(jsonstr)) fmt.Println("Without StringAsNumber():") n, err := j.GetInt("timestamp") fmt.Println(n, err) fmt.Println("With StringAsNumber():") j.StringAsNumber() n, err = j.GetInt("timestamp") fmt.Println(n, err)
Output: Without StringAsNumber(): 0 type mismatch: expected NUMBER, found STRING With StringAsNumber(): 1438194274 <nil>