2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-03-21 19:31:58 +08:00
|
|
|
//
|
|
|
|
|
// 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"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
2020-03-21 19:31:58 +08:00
|
|
|
)
|
|
|
|
|
|
2022-02-15 22:08:51 +08:00
|
|
|
func ExampleNew() {
|
2020-03-21 19:31:58 +08:00
|
|
|
jsonContent := `{"name":"john", "score":"100"}`
|
|
|
|
|
j := gjson.New(jsonContent)
|
|
|
|
|
fmt.Println(j.Get("name"))
|
|
|
|
|
fmt.Println(j.Get("score"))
|
|
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
|
// john
|
|
|
|
|
// 100
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 22:08:51 +08:00
|
|
|
func ExampleNewWithTag() {
|
2020-03-21 19:31:58 +08:00
|
|
|
type Me struct {
|
|
|
|
|
Name string `tag:"name"`
|
|
|
|
|
Score int `tag:"score"`
|
|
|
|
|
Title string
|
|
|
|
|
}
|
|
|
|
|
me := Me{
|
|
|
|
|
Name: "john",
|
|
|
|
|
Score: 100,
|
|
|
|
|
Title: "engineer",
|
|
|
|
|
}
|
2022-02-23 00:46:13 +08:00
|
|
|
j := gjson.NewWithTag(me, "tag", true)
|
2020-03-21 19:31:58 +08:00
|
|
|
fmt.Println(j.Get("name"))
|
|
|
|
|
fmt.Println(j.Get("score"))
|
|
|
|
|
fmt.Println(j.Get("Title"))
|
2022-02-12 17:27:32 +08:00
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
|
// john
|
|
|
|
|
// 100
|
2022-02-15 22:08:51 +08:00
|
|
|
// engineer
|
2022-02-12 17:27:32 +08:00
|
|
|
}
|
|
|
|
|
|
2022-02-15 22:08:51 +08:00
|
|
|
func ExampleNewWithOptions() {
|
2022-02-12 17:27:32 +08:00
|
|
|
type Me struct {
|
|
|
|
|
Name string `tag:"name"`
|
|
|
|
|
Score int `tag:"score"`
|
|
|
|
|
Title string
|
|
|
|
|
}
|
|
|
|
|
me := Me{
|
|
|
|
|
Name: "john",
|
|
|
|
|
Score: 100,
|
|
|
|
|
Title: "engineer",
|
|
|
|
|
}
|
2022-02-15 22:08:51 +08:00
|
|
|
|
|
|
|
|
j := gjson.NewWithOptions(me, gjson.Options{
|
|
|
|
|
Tags: "tag",
|
|
|
|
|
})
|
2022-02-12 17:27:32 +08:00
|
|
|
fmt.Println(j.Get("name"))
|
|
|
|
|
fmt.Println(j.Get("score"))
|
|
|
|
|
fmt.Println(j.Get("Title"))
|
|
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
|
// john
|
|
|
|
|
// 100
|
|
|
|
|
// engineer
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 15:29:16 +08:00
|
|
|
func ExampleNewWithOptions_jsonContent() {
|
2022-02-24 21:14:11 +08:00
|
|
|
jsonContent := `{"name":"john", "score":"100"}`
|
|
|
|
|
|
|
|
|
|
content := make([]byte, 3, len(jsonContent)+3)
|
|
|
|
|
content[0] = 0xEF
|
|
|
|
|
content[1] = 0xBB
|
|
|
|
|
content[2] = 0xBF
|
2022-02-24 21:41:39 +08:00
|
|
|
content = append(content, jsonContent...)
|
2022-02-24 21:14:11 +08:00
|
|
|
|
|
|
|
|
j := gjson.NewWithOptions(content, gjson.Options{
|
|
|
|
|
Tags: "tag",
|
|
|
|
|
})
|
|
|
|
|
fmt.Println(j.Get("name"))
|
|
|
|
|
fmt.Println(j.Get("score"))
|
|
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
|
// john
|
|
|
|
|
// 100
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 15:29:16 +08:00
|
|
|
func ExampleNew_xmlContent() {
|
|
|
|
|
xmlContent := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>`
|
|
|
|
|
j := gjson.New(xmlContent)
|
2022-02-15 22:08:51 +08:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 15:29:16 +08:00
|
|
|
func ExampleNew_me() {
|
2022-02-12 17:27:32 +08:00
|
|
|
type Me struct {
|
2022-02-15 22:08:51 +08:00
|
|
|
Name string `json:"name"`
|
|
|
|
|
Score int `json:"score"`
|
2022-02-12 17:27:32 +08:00
|
|
|
}
|
|
|
|
|
me := Me{
|
|
|
|
|
Name: "john",
|
|
|
|
|
Score: 100,
|
|
|
|
|
}
|
2022-02-15 22:08:51 +08:00
|
|
|
j := gjson.New(me)
|
2022-02-12 17:27:32 +08:00
|
|
|
fmt.Println(j.Get("name"))
|
|
|
|
|
fmt.Println(j.Get("score"))
|
|
|
|
|
// Output:
|
|
|
|
|
// john
|
|
|
|
|
// 100
|
|
|
|
|
}
|