mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
1.SetViolenceCheck 2.ToJson 3.ToJsonString 4.ToJsonIndent 5.ToJsonIndentString 6.MustToJson 7.MustToJsonString 8.MustToJsonIndent 9.MustToJsonIndentString 10.ToXml 11.ToXmlString 12.ToXmlIndent 13.ToXmlIndentString 14.MustToXml 15.MustToXmlString 16.MustToXmlIndent 17.MustToXmlIndentString 18.ToYaml 19.ToYamlString 20.ToYamlIndent 21.MustToYaml 22.MustToYamlString 23.ToToml 24.ToTomlString 25.MustToToml 26.MustToTomlString 27.ToIni 28.ToIniString 29.MustToIni 30.MustToIniString 31.MarshalJSON 32.UnmarshalJSON 33.UnmarshalValue
100 lines
1.8 KiB
Go
100 lines
1.8 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gjson_test
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
)
|
|
|
|
func ExampleNew() {
|
|
jsonContent := `{"name":"john", "score":"100"}`
|
|
j := gjson.New(jsonContent)
|
|
fmt.Println(j.Get("name"))
|
|
fmt.Println(j.Get("score"))
|
|
|
|
// Output:
|
|
// john
|
|
// 100
|
|
}
|
|
|
|
func ExampleNewWithTag() {
|
|
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")
|
|
fmt.Println(j.Get("name"))
|
|
fmt.Println(j.Get("score"))
|
|
fmt.Println(j.Get("Title"))
|
|
|
|
// Output:
|
|
// john
|
|
// 100
|
|
// engineer
|
|
}
|
|
|
|
func ExampleNewWithOptions() {
|
|
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 ExampleNew_Xml() {
|
|
jsonContent := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>`
|
|
j := gjson.New(jsonContent)
|
|
// Note that there's root node in the XML content.
|
|
fmt.Println(j.Get("doc.name"))
|
|
fmt.Println(j.Get("doc.score"))
|
|
// Output:
|
|
// john
|
|
// 100
|
|
}
|
|
|
|
func ExampleNew_Struct() {
|
|
type Me struct {
|
|
Name string `json:"name"`
|
|
Score int `json:"score"`
|
|
}
|
|
me := Me{
|
|
Name: "john",
|
|
Score: 100,
|
|
}
|
|
j := gjson.New(me)
|
|
fmt.Println(j.Get("name"))
|
|
fmt.Println(j.Get("score"))
|
|
// Output:
|
|
// john
|
|
// 100
|
|
}
|