Documentation ¶
Overview ¶
Package gjson provides convenient API for JSON/XML/INI/YAML/TOML data handling.
Example (ConversionNormalFormats) ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 1, "array" : ["John", "Ming"] } }` if j, err := gjson.DecodeToJson(data); err != nil { panic(err) } else { fmt.Println("JSON:") fmt.Println(j.MustToJsonString()) fmt.Println("======================") fmt.Println("XML:") fmt.Println(j.MustToXmlString()) fmt.Println("======================") fmt.Println("YAML:") fmt.Println(j.MustToYamlString()) fmt.Println("======================") fmt.Println("TOML:") fmt.Println(j.MustToTomlString()) } }
Output: JSON: {"users":{"array":["John","Ming"],"count":1}} ====================== XML: <users><array>John</array><array>Ming</array><count>1</count></users> ====================== YAML: users: array: - John - Ming count: 1 ====================== TOML: [users] array = ["John", "Ming"] count = 1.0
Index ¶
- Constants
- func Decode(data interface{}, options ...Options) (interface{}, error)
- func DecodeTo(data interface{}, v interface{}, options ...Options) (err error)
- func Encode(value interface{}) ([]byte, error)
- func EncodeString(value interface{}) (string, error)
- func IsValidDataType(dataType string) bool
- func Marshal(v interface{}) (marshaledBytes []byte, err error)
- func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error)
- func MustEncode(value interface{}) []byte
- func MustEncodeString(value interface{}) string
- func Unmarshal(data []byte, v interface{}) (err error)
- func Valid(data interface{}) bool
- type Json
- func DecodeToJson(data interface{}, options ...Options) (*Json, error)
- func Load(path string, safe ...bool) (*Json, error)
- func LoadContent(data interface{}, safe ...bool) (*Json, error)
- func LoadContentType(dataType string, data interface{}, safe ...bool) (*Json, error)
- func LoadIni(data interface{}, safe ...bool) (*Json, error)
- func LoadJson(data interface{}, safe ...bool) (*Json, error)
- func LoadProperties(data interface{}, safe ...bool) (*Json, error)
- func LoadToml(data interface{}, safe ...bool) (*Json, error)
- func LoadWithOptions(data interface{}, options Options) (*Json, error)
- func LoadXml(data interface{}, safe ...bool) (*Json, error)
- func LoadYaml(data interface{}, safe ...bool) (*Json, error)
- func New(data interface{}, safe ...bool) *Json
- func NewWithOptions(data interface{}, options Options) *Json
- func NewWithTag(data interface{}, tags string, safe ...bool) *Json
- func (j *Json) Append(pattern string, value interface{}) error
- func (j *Json) Array() []interface{}
- func (j *Json) Contains(pattern string) bool
- func (j *Json) Dump()
- func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var
- func (j *Json) GetJson(pattern string, def ...interface{}) *Json
- func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json
- func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json
- func (j *Json) Interface() interface{}
- func (j *Json) Interfaces() []interface{}
- func (j *Json) IsNil() bool
- func (j *Json) Len(pattern string) int
- func (j *Json) Map() map[string]interface{}
- func (j *Json) MapStrAny() map[string]interface{}
- func (j Json) MarshalJSON() ([]byte, error)
- func (j *Json) MustAppend(pattern string, value interface{})
- func (j *Json) MustRemove(pattern string)
- func (j *Json) MustSet(pattern string, value interface{})
- func (j *Json) MustToIni() []byte
- func (j *Json) MustToIniString() string
- func (j *Json) MustToJson() []byte
- func (j *Json) MustToJsonIndent() []byte
- func (j *Json) MustToJsonIndentString() string
- func (j *Json) MustToJsonString() string
- func (j *Json) MustToProperties() []byte
- func (j *Json) MustToPropertiesString() string
- func (j *Json) MustToToml() []byte
- func (j *Json) MustToTomlString() string
- func (j *Json) MustToXml(rootTag ...string) []byte
- func (j *Json) MustToXmlIndent(rootTag ...string) []byte
- func (j *Json) MustToXmlIndentString(rootTag ...string) string
- func (j *Json) MustToXmlString(rootTag ...string) string
- func (j *Json) MustToYaml() []byte
- func (j *Json) MustToYamlString() string
- func (j *Json) Remove(pattern string) error
- func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error
- func (j *Json) Set(pattern string, value interface{}) error
- func (j *Json) SetSplitChar(char byte)
- func (j *Json) SetViolenceCheck(enabled bool)
- func (j *Json) ToIni() ([]byte, error)
- func (j *Json) ToIniString() (string, error)
- func (j *Json) ToJson() ([]byte, error)
- func (j *Json) ToJsonIndent() ([]byte, error)
- func (j *Json) ToJsonIndentString() (string, error)
- func (j *Json) ToJsonString() (string, error)
- func (j *Json) ToProperties() ([]byte, error)
- func (j *Json) ToPropertiesString() (string, error)
- func (j *Json) ToToml() ([]byte, error)
- func (j *Json) ToTomlString() (string, error)
- func (j *Json) ToXml(rootTag ...string) ([]byte, error)
- func (j *Json) ToXmlIndent(rootTag ...string) ([]byte, error)
- func (j *Json) ToXmlIndentString(rootTag ...string) (string, error)
- func (j *Json) ToXmlString(rootTag ...string) (string, error)
- func (j *Json) ToYaml() ([]byte, error)
- func (j *Json) ToYamlIndent(indent string) ([]byte, error)
- func (j *Json) ToYamlString() (string, error)
- func (j *Json) UnmarshalJSON(b []byte) error
- func (j *Json) UnmarshalValue(value interface{}) error
- func (j *Json) Var() *gvar.Var
- type Options
Examples ¶
- Package (ConversionNormalFormats)
- Decode
- DecodeTo
- DecodeToJson
- Encode
- EncodeString
- IsValidDataType
- Json.Append
- Json.Array
- Json.Contains
- Json.Dump
- Json.Get
- Json.GetJson
- Json.GetJsonMap
- Json.GetJsons
- Json.Interface
- Json.Interfaces
- Json.IsNil
- Json.Len
- Json.Map
- Json.MapStrAny
- Json.MarshalJSON
- Json.MustAppend
- Json.MustRemove
- Json.MustSet
- Json.MustToIni
- Json.MustToIniString
- Json.MustToJson
- Json.MustToJsonIndent
- Json.MustToJsonIndentString
- Json.MustToJsonString
- Json.MustToProperties
- Json.MustToPropertiesString
- Json.MustToToml
- Json.MustToTomlString
- Json.MustToXml
- Json.MustToXmlIndent
- Json.MustToXmlIndentString
- Json.MustToXmlString
- Json.MustToYaml
- Json.MustToYamlString
- Json.Remove
- Json.Scan
- Json.Set
- Json.SetSplitChar
- Json.SetViolenceCheck
- Json.ToIni
- Json.ToIniString
- Json.ToJson
- Json.ToJsonIndent
- Json.ToJsonIndentString
- Json.ToJsonString
- Json.ToProperties
- Json.ToPropertiesString
- Json.ToToml
- Json.ToTomlString
- Json.ToXml
- Json.ToXmlIndent
- Json.ToXmlIndentString
- Json.ToXmlString
- Json.ToYaml
- Json.ToYamlIndent
- Json.ToYamlString
- Json.UnmarshalJSON
- Json.Var
- Load
- LoadContent
- LoadContentType
- LoadIni
- LoadJson
- LoadToml
- LoadXml
- LoadYaml
- Marshal
- MarshalIndent
- MustEncode
- MustEncodeString
- New
- NewWithOptions
- NewWithTag
- Unmarshal
- Valid
Constants ¶
const ( ContentTypeJson = `json` ContentTypeJs = `js` ContentTypeXml = `xml` ContentTypeIni = `ini` ContentTypeYaml = `yaml` ContentTypeYml = `yml` ContentTypeToml = `toml` ContentTypeProperties = `properties` )
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode decodes json format `data` to golang variable. The parameter `data` can be either bytes or string type.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { jsonContent := `{"name":"john","score":100}` info, _ := gjson.Decode([]byte(jsonContent)) fmt.Println(info) }
Output: map[name:john score:100]
func DecodeTo ¶
DecodeTo decodes json format `data` to specified golang variable `v`. The parameter `data` can be either bytes or string type. The parameter `v` should be a pointer type.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Score int } var info BaseInfo jsonContent := "{\"name\":\"john\",\"score\":100}" gjson.DecodeTo([]byte(jsonContent), &info) fmt.Printf("%+v", info) }
Output: {Name:john Score:100}
func Encode ¶
Encode encodes any golang variable `value` to JSON bytes.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } infoData, _ := gjson.Encode(info) fmt.Println(string(infoData)) }
Output: {"Name":"John","Age":18}
func EncodeString ¶
EncodeString encodes any golang variable `value` to JSON string.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } infoData, _ := gjson.EncodeString(info) fmt.Println(infoData) }
Output: {"Name":"John","Age":18}
func IsValidDataType ¶
IsValidDataType checks and returns whether given `dataType` a valid data type for loading.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { fmt.Println(gjson.IsValidDataType("json")) fmt.Println(gjson.IsValidDataType("yml")) fmt.Println(gjson.IsValidDataType("js")) fmt.Println(gjson.IsValidDataType("mp4")) fmt.Println(gjson.IsValidDataType("xsl")) fmt.Println(gjson.IsValidDataType("txt")) fmt.Println(gjson.IsValidDataType("")) fmt.Println(gjson.IsValidDataType(".json")) fmt.Println(gjson.IsValidDataType(".properties")) }
Output: true true true false false false false true true
func Marshal ¶
Marshal is alias of Encode in order to fit the habit of json.Marshal/Unmarshal functions.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := map[string]interface{}{ "name": "john", "score": 100, } jsonData, _ := gjson.Marshal(data) fmt.Println(string(jsonData)) type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "Guo Qiang", Age: 18, } infoData, _ := gjson.Marshal(info) fmt.Println(string(infoData)) }
Output: {"name":"john","score":100} {"Name":"Guo Qiang","Age":18}
func MarshalIndent ¶
MarshalIndent is alias of json.MarshalIndent in order to fit the habit of json.MarshalIndent function.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } infoData, _ := gjson.MarshalIndent(info, "", "\t") fmt.Println(string(infoData)) }
Output: { "Name": "John", "Age": 18 }
func MustEncode ¶
func MustEncode(value interface{}) []byte
MustEncode performs as Encode, but it panics if any error occurs.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } infoData := gjson.MustEncode(info) fmt.Println(string(infoData)) }
Output: {"Name":"John","Age":18}
func MustEncodeString ¶
func MustEncodeString(value interface{}) string
MustEncodeString encodes any golang variable `value` to JSON string. It panics if any error occurs.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } infoData := gjson.MustEncodeString(info) fmt.Println(infoData) }
Output: {"Name":"John","Age":18}
func Unmarshal ¶
Unmarshal is alias of DecodeTo in order to fit the habit of json.Marshal/Unmarshal functions.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Score int } var info BaseInfo jsonContent := "{\"name\":\"john\",\"score\":100}" gjson.Unmarshal([]byte(jsonContent), &info) fmt.Printf("%+v", info) }
Output: {Name:john Score:100}
func Valid ¶
func Valid(data interface{}) bool
Valid checks whether `data` is a valid JSON data type. The parameter `data` specifies the json format data, which can be either bytes or string type.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`) fmt.Println(gjson.Valid(data1)) fmt.Println(gjson.Valid(data2)) }
Output: true false
Types ¶
type Json ¶
type Json struct {
// contains filtered or unexported fields
}
Json is the customized JSON struct.
func DecodeToJson ¶
DecodeToJson codes json format `data` to a Json object. The parameter `data` can be either bytes or string type.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { jsonContent := `{"name":"john","score":100}"` j, _ := gjson.DecodeToJson([]byte(jsonContent)) fmt.Println(j.Map()) // May Output: // map[name:john score:100] }
Output:
func Load ¶
Load loads content from specified file `path`, and creates a Json object from its content.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/test/gtest" ) func main() { jsonFilePath := gtest.DataPath("json", "data1.json") j, _ := gjson.Load(jsonFilePath) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) notExistFilePath := gtest.DataPath("json", "data2.json") j2, _ := gjson.Load(notExistFilePath) fmt.Println(j2.Get("name")) }
Output: john 100
func LoadContent ¶
LoadContent creates a Json object from given content, it checks the data type of `content` automatically, supporting data content type as follows: JSON, XML, INI, YAML and TOML.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { jsonContent := `{"name":"john", "score":"100"}` j, _ := gjson.LoadContent(jsonContent) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) }
Output: john 100
func LoadContentType ¶
LoadContentType creates a Json object from given type and content, supporting data content type as follows: JSON, XML, INI, YAML and TOML.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { jsonContent := `{"name":"john", "score":"100"}` xmlContent := `<?xml version="1.0" encoding="UTF-8"?> <base> <name>john</name> <score>100</score> </base>` j, _ := gjson.LoadContentType("json", jsonContent) x, _ := gjson.LoadContentType("xml", xmlContent) j1, _ := gjson.LoadContentType("json", "") fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) fmt.Println(x.Get("base.name")) fmt.Println(x.Get("base.score")) fmt.Println(j1.Get("")) }
Output: john 100 john 100
func LoadIni ¶
LoadIni creates a Json object from given INI format content.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { iniContent := ` [base] name = john score = 100 ` j, _ := gjson.LoadIni(iniContent) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) }
Output: john 100
func LoadJson ¶
LoadJson creates a Json object from given JSON format content.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { jsonContent := `{"name":"john", "score":"100"}` j, _ := gjson.LoadJson(jsonContent) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) }
Output: john 100
func LoadProperties ¶ added in v2.1.0
LoadProperties creates a Json object from given TOML format content.
func LoadToml ¶
LoadToml creates a Json object from given TOML format content.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { tomlContent := `[base] name = "john" score = 100` j, _ := gjson.LoadToml(tomlContent) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) }
Output: john 100
func LoadWithOptions ¶ added in v2.1.0
LoadWithOptions creates a Json object from given JSON format content and options.
func LoadXml ¶
LoadXml creates a Json object from given XML format content.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { xmlContent := `<?xml version="1.0" encoding="UTF-8"?> <base> <name>john</name> <score>100</score> </base>` j, _ := gjson.LoadXml(xmlContent) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) }
Output: john 100
func LoadYaml ¶
LoadYaml creates a Json object from given YAML format content.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { yamlContent := `base: name: john score: 100` j, _ := gjson.LoadYaml(yamlContent) fmt.Println(j.Get("base.name")) fmt.Println(j.Get("base.score")) }
Output: john 100
func New ¶
New creates a Json object with any variable type of `data`, but `data` should be a map or slice for data access reason, or it will make no sense.
The parameter `safe` specifies whether using this Json object in concurrent-safe context, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { jsonContent := `{"name":"john", "score":"100"}` j := gjson.New(jsonContent) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) }
Output: john 100
func NewWithOptions ¶
NewWithOptions creates a Json object with any variable type of `data`, but `data` should be a map or slice for data access reason, or it will make no sense.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type Me struct { Name string `tag:"name"` Score int `tag:"score"` Title string } me := Me{ Name: "john", Score: 100, Title: "engineer", } j := gjson.NewWithOptions(me, gjson.Options{ Tags: "tag", }) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) fmt.Println(j.Get("Title")) }
Output: john 100 engineer
func NewWithTag ¶
NewWithTag creates a Json object with any variable type of `data`, but `data` should be a map or slice for data access reason, or it will make no sense.
The parameter `tags` specifies priority tags for struct conversion to map, multiple tags joined with char ','.
The parameter `safe` specifies whether using this Json object in concurrent-safe context, which is false in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type Me struct { Name string `tag:"name"` Score int `tag:"score"` Title string } me := Me{ Name: "john", Score: 100, Title: "engineer", } j := gjson.NewWithTag(me, "tag", true) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) fmt.Println(j.Get("Title")) }
Output: john 100 engineer
func (*Json) Append ¶
Append appends value to the value by specified `pattern`. The target value by `pattern` should be type of slice.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 1, "array" : ["John", "Ming"] } }` j, _ := gjson.LoadContent(data) j.Append("users.array", "Lily") fmt.Println(j.Get("users.array").Array()) }
Output: [John Ming Lily]
func (*Json) Array ¶
func (j *Json) Array() []interface{}
Array converts current Json object to []interface{}. It returns nil if fails.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 1, "array" : ["John", "Ming"] } }` j, _ := gjson.LoadContent(data) fmt.Println(j.Get("users.array")) }
Output: ["John","Ming"]
func (*Json) Contains ¶
Contains checks whether the value by specified `pattern` exist.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) fmt.Println(j.Contains("Age")) fmt.Println(j.Contains("Addr")) }
Output: true false
func (*Json) Dump ¶
func (j *Json) Dump()
Dump prints current Json object with more manually readable.
Example ¶
package main import ( "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{"name":"john","age":"18"}` j, _ := gjson.LoadContent(data) j.Dump() // May Output: //{ // "name": "john", // "age": "18", //} }
Output:
func (*Json) Get ¶
Get retrieves and returns value by specified `pattern`. It returns all values of current Json object if `pattern` is given ".". It returns nil if no value found by `pattern`.
We can also access slice item by its index number in `pattern` like: "list.10", "array.0.name", "array.0.1.id".
It returns a default value specified by `def` if value for `pattern` is not found.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 1, "array" : ["John", "Ming"] } }` j, _ := gjson.LoadContent(data) fmt.Println(j.Get(".")) fmt.Println(j.Get("users")) fmt.Println(j.Get("users.count")) fmt.Println(j.Get("users.array")) var nilJ *gjson.Json = nil fmt.Println(nilJ.Get(".")) }
Output: {"users":{"array":["John","Ming"],"count":1}} {"array":["John","Ming"],"count":1} 1 ["John","Ming"]
func (*Json) GetJson ¶
GetJson gets the value by specified `pattern`, and converts it to an un-concurrent-safe Json object.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 1, "array" : ["John", "Ming"] } }` j, _ := gjson.LoadContent(data) fmt.Println(j.GetJson("users.array").Array()) }
Output: [John Ming]
func (*Json) GetJsonMap ¶
GetJsonMap gets the value by specified `pattern`, and converts it to a map of un-concurrent-safe Json object.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 1, "array" : { "info" : {"Age":18,"Name":"John"}, "addr" : {"City":"Chengdu","Company":"Tencent"} } } }` j, _ := gjson.LoadContent(data) jsonMap := j.GetJsonMap("users.array") for _, json := range jsonMap { fmt.Println(json.Interface()) } // May Output: // map[City:Chengdu Company:Tencent] // map[Age:18 Name:John] }
Output:
func (*Json) GetJsons ¶
GetJsons gets the value by specified `pattern`, and converts it to a slice of un-concurrent-safe Json object.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 3, "array" : [{"Age":18,"Name":"John"}, {"Age":20,"Name":"Tom"}] } }` j, _ := gjson.LoadContent(data) jsons := j.GetJsons("users.array") for _, json := range jsons { fmt.Println(json.Interface()) } }
Output: map[Age:18 Name:John] map[Age:20 Name:Tom]
func (*Json) Interface ¶
func (j *Json) Interface() interface{}
Interface returns the json value.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) fmt.Println(j.Interface()) var nilJ *gjson.Json = nil fmt.Println(nilJ.Interface()) }
Output: map[Age:18 Name:John] <nil>
func (*Json) Interfaces ¶
func (j *Json) Interfaces() []interface{}
Interfaces implements interface function Interfaces().
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } infoList := []BaseInfo{ BaseInfo{ Name: "John", Age: 18, }, BaseInfo{ Name: "Tom", Age: 20, }, } j := gjson.New(infoList) fmt.Println(j.Interfaces()) }
Output: [{John 18} {Tom 20}]
func (*Json) IsNil ¶
IsNil checks whether the value pointed by `j` is nil.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data1 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`) data2 := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`) j1, _ := gjson.LoadContent(data1) fmt.Println(j1.IsNil()) j2, _ := gjson.LoadContent(data2) fmt.Println(j2.IsNil()) }
Output: false true
func (*Json) Len ¶
Len returns the length/size of the value by specified `pattern`. The target value by `pattern` should be type of slice or map. It returns -1 if the target value is not found, or its type is invalid.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 1, "nameArray" : ["Join", "Tom"], "infoMap" : { "name" : "Join", "age" : 18, "addr" : "ChengDu" } } }` j, _ := gjson.LoadContent(data) fmt.Println(j.Len("users.nameArray")) fmt.Println(j.Len("users.infoMap")) }
Output: 2 3
func (*Json) Map ¶
Map converts current Json object to map[string]interface{}. It returns nil if fails.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 1, "info" : { "name" : "John", "age" : 18, "addr" : "ChengDu" } } }` j, _ := gjson.LoadContent(data) fmt.Println(j.Get("users.info").Map()) }
Output: map[addr:ChengDu age:18 name:John]
func (*Json) MapStrAny ¶
MapStrAny implements interface function MapStrAny().
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) fmt.Println(j.MapStrAny()) }
Output: map[Age:18 Name:John]
func (Json) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) jsonBytes, _ := j.MarshalJSON() fmt.Println(string(jsonBytes)) }
Output: {"Age":18,"Name":"John"}
func (*Json) MustAppend ¶
MustAppend performs as Append, but it panics if any error occurs.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 1, "array" : ["John", "Ming"] } }` j, _ := gjson.LoadContent(data) j.MustAppend("users.array", "Lily") fmt.Println(j.Get("users.array").Array()) }
Output: [John Ming Lily]
func (*Json) MustRemove ¶
MustRemove performs as Remove, but it panics if any error occurs.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) j.MustRemove("Age") fmt.Println(j.Var().String()) }
Output: {"Name":"John"}
func (*Json) MustSet ¶
MustSet performs as Set, but it panics if any error occurs.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) j.MustSet("Addr", "ChengDu") fmt.Println(j.Var().String()) }
Output: {"Addr":"ChengDu","Age":18,"Name":"John"}
func (*Json) MustToIni ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string } info := BaseInfo{ Name: "John", } j := gjson.New(info) IniBytes := j.MustToIni() fmt.Println(string(IniBytes)) }
Output: Name=John
func (*Json) MustToIniString ¶
MustToIniString .
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string } info := BaseInfo{ Name: "John", } j := gjson.New(info) IniStr := j.MustToIniString() fmt.Println(string(IniStr)) }
Output: Name=John
func (*Json) MustToJson ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) jsonBytes := j.MustToJson() fmt.Println(string(jsonBytes)) }
Output: {"Age":18,"Name":"John"}
func (*Json) MustToJsonIndent ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) jsonBytes := j.MustToJsonIndent() fmt.Println(string(jsonBytes)) }
Output: { "Age": 18, "Name": "John" }
func (*Json) MustToJsonIndentString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) jsonStr := j.MustToJsonIndentString() fmt.Println(jsonStr) }
Output: { "Age": 18, "Name": "John" }
func (*Json) MustToJsonString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) jsonStr := j.MustToJsonString() fmt.Println(jsonStr) }
Output: {"Age":18,"Name":"John"}
func (*Json) MustToProperties ¶ added in v2.1.0
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string } info := BaseInfo{ Name: "John", } j := gjson.New(info) pr := j.MustToProperties() fmt.Println(string(pr)) }
Output: name = John
func (*Json) MustToPropertiesString ¶ added in v2.1.0
MustTopropertiesString
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string } info := BaseInfo{ Name: "John", } j := gjson.New(info) pr := j.MustToPropertiesString() fmt.Println(pr) }
Output: name = John
func (*Json) MustToToml ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) TomlBytes := j.MustToToml() fmt.Println(string(TomlBytes)) }
Output: Age = 18 Name = "John"
func (*Json) MustToTomlString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) TomlStr := j.MustToTomlString() fmt.Println(string(TomlStr)) }
Output: Age = 18 Name = "John"
func (*Json) MustToXml ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) xmlBytes := j.MustToXml() fmt.Println(string(xmlBytes)) }
Output: <doc><Age>18</Age><Name>John</Name></doc>
func (*Json) MustToXmlIndent ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) xmlBytes := j.MustToXmlIndent() fmt.Println(string(xmlBytes)) }
Output: <doc> <Age>18</Age> <Name>John</Name> </doc>
func (*Json) MustToXmlIndentString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) xmlStr := j.MustToXmlIndentString() fmt.Println(string(xmlStr)) }
Output: <doc> <Age>18</Age> <Name>John</Name> </doc>
func (*Json) MustToXmlString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) xmlStr := j.MustToXmlString() fmt.Println(string(xmlStr)) }
Output: <doc><Age>18</Age><Name>John</Name></doc>
func (*Json) MustToYaml ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) YamlBytes := j.MustToYaml() fmt.Println(string(YamlBytes)) }
Output: Age: 18 Name: John
func (*Json) MustToYamlString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) YamlStr := j.MustToYamlString() fmt.Println(string(YamlStr)) }
Output: Age: 18 Name: John
func (*Json) Remove ¶
Remove deletes value with specified `pattern`. It supports hierarchical data access by char separator, which is '.' in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) j.Remove("Age") fmt.Println(j.Var().String()) }
Output: {"Name":"John"}
func (*Json) Scan ¶
Scan automatically calls Struct or Structs function according to the type of parameter `pointer` to implement the converting.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{"name":"john","age":"18"}` type BaseInfo struct { Name string Age int } info := BaseInfo{} j, _ := gjson.LoadContent(data) j.Scan(&info) fmt.Println(info) // May Output: // {john 18} }
Output:
func (*Json) Set ¶
Set sets value with specified `pattern`. It supports hierarchical data access by char separator, which is '.' in default.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) j.Set("Addr", "ChengDu") j.Set("Friends.0", "Tom") fmt.Println(j.Var().String()) }
Output: {"Addr":"ChengDu","Age":18,"Friends":["Tom"],"Name":"John"}
func (*Json) SetSplitChar ¶
SetSplitChar sets the separator char for hierarchical data access.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 2, "list" : [ {"name" : "Ming", "score" : 60}, {"name" : "John", "score" : 99.5} ] } }` if j, err := gjson.DecodeToJson(data); err != nil { panic(err) } else { j.SetSplitChar('#') fmt.Println("John Score:", j.Get("users#list#1#score").Float32()) } }
Output: John Score: 99.5
func (*Json) SetViolenceCheck ¶
SetViolenceCheck enables/disables violence check for hierarchical data access.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { data := `{ "users" : { "count" : 100 }, "users.count" : 101 }` if j, err := gjson.DecodeToJson(data); err != nil { fmt.Println(err) } else { j.SetViolenceCheck(false) fmt.Println("Users Count:", j.Get("users.count")) j.SetViolenceCheck(true) fmt.Println("Users Count:", j.Get("users.count")) } }
Output: Users Count: 100 Users Count: 101
func (*Json) ToIni ¶
ToIni json to ini
Example ¶
======================================================================== INI ========================================================================
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) IniBytes, _ := j.ToIni() fmt.Println(string(IniBytes)) // May Output: //Name=John //Age=18 }
Output:
func (*Json) ToIniString ¶
ToIniString ini to string
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string } info := BaseInfo{ Name: "John", } j := gjson.New(info) IniStr, _ := j.ToIniString() fmt.Println(string(IniStr)) }
Output: Name=John
func (*Json) ToJson ¶
Example ¶
======================================================================== JSON ========================================================================
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) jsonBytes, _ := j.ToJson() fmt.Println(string(jsonBytes)) }
Output: {"Age":18,"Name":"John"}
func (*Json) ToJsonIndent ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) jsonBytes, _ := j.ToJsonIndent() fmt.Println(string(jsonBytes)) }
Output: { "Age": 18, "Name": "John" }
func (*Json) ToJsonIndentString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) jsonStr, _ := j.ToJsonIndentString() fmt.Println(jsonStr) }
Output: { "Age": 18, "Name": "John" }
func (*Json) ToJsonString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) jsonStr, _ := j.ToJsonString() fmt.Println(jsonStr) }
Output: {"Age":18,"Name":"John"}
func (*Json) ToProperties ¶ added in v2.1.0
======================================================================== properties ======================================================================== Toproperties json to properties
Example ¶
======================================================================== Properties ========================================================================
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) pr, _ := j.ToProperties() fmt.Println(string(pr)) // May Output: // name = John // age = 18 }
Output:
func (*Json) ToPropertiesString ¶ added in v2.1.0
TopropertiesString properties to string
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string } info := BaseInfo{ Name: "John", } j := gjson.New(info) pr, _ := j.ToPropertiesString() fmt.Println(pr) }
Output: name = John
func (*Json) ToToml ¶
Example ¶
======================================================================== TOML ========================================================================
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) TomlBytes, _ := j.ToToml() fmt.Println(string(TomlBytes)) }
Output: Age = 18 Name = "John"
func (*Json) ToTomlString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) TomlStr, _ := j.ToTomlString() fmt.Println(string(TomlStr)) }
Output: Age = 18 Name = "John"
func (*Json) ToXml ¶
Example ¶
======================================================================== XML ========================================================================
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) xmlBytes, _ := j.ToXml() fmt.Println(string(xmlBytes)) }
Output: <doc><Age>18</Age><Name>John</Name></doc>
func (*Json) ToXmlIndent ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) xmlBytes, _ := j.ToXmlIndent() fmt.Println(string(xmlBytes)) }
Output: <doc> <Age>18</Age> <Name>John</Name> </doc>
func (*Json) ToXmlIndentString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) xmlStr, _ := j.ToXmlIndentString() fmt.Println(string(xmlStr)) }
Output: <doc> <Age>18</Age> <Name>John</Name> </doc>
func (*Json) ToXmlString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) xmlStr, _ := j.ToXmlString() fmt.Println(string(xmlStr)) }
Output: <doc><Age>18</Age><Name>John</Name></doc>
func (*Json) ToYaml ¶
Example ¶
======================================================================== YAML ========================================================================
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) YamlBytes, _ := j.ToYaml() fmt.Println(string(YamlBytes)) }
Output: Age: 18 Name: John
func (*Json) ToYamlIndent ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) YamlBytes, _ := j.ToYamlIndent("") fmt.Println(string(YamlBytes)) }
Output: Age: 18 Name: John
func (*Json) ToYamlString ¶
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) YamlStr, _ := j.ToYamlString() fmt.Println(string(YamlStr)) }
Output: Age: 18 Name: John
func (*Json) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { jsonStr := `{"Age":18,"Name":"John"}` j := gjson.New("") j.UnmarshalJSON([]byte(jsonStr)) fmt.Println(j.Map()) }
Output: map[Age:18 Name:John]
func (*Json) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for Json.
func (*Json) Var ¶
Var returns the json value as *gvar.Var.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/v2/encoding/gjson" ) func main() { type BaseInfo struct { Name string Age int } info := BaseInfo{ Name: "John", Age: 18, } j := gjson.New(info) fmt.Println(j.Var().String()) fmt.Println(j.Var().Map()) }
Output: {"Age":18,"Name":"John"} map[Age:18 Name:John]
type Options ¶
type Options struct { Safe bool // Mark this object is for in concurrent-safe usage. This is especially for Json object creating. Tags string // Custom priority tags for decoding, eg: "json,yaml,MyTag". This is especially for struct parsing into Json object. Type string // Type specifies the data content type, eg: json, xml, yaml, toml, ini. StrNumber bool // StrNumber causes the Decoder to unmarshal a number into an interface{} as a string instead of as a float64. }
Options for Json object creating/loading.