Files
gf/encoding/gjson/gjson_z_example_dataset_test.go
houseme 2d5fcd73cb chore: upgrade dependencies to latest versions and fix security vulne… (#4237)
This PR includes the following updates and fixes:

- **Dependency upgrades**: Updated all dependencies in `go.mod` to their
latest versions to ensure compatibility and leverage the latest features
and fixes.
- **Security fixes**:
- Resolved known vulnerabilities in `golang.org/x/net` by upgrading to
the latest secure version.
- Addressed security issues in `golang.org/x/crypto` by upgrading to the
latest secure version.

These changes improve the overall security and stability of the project.
Please review the changes and ensure compatibility with the updated
dependencies.

---------

Co-authored-by: hailaz <739476267@qq.com>
2025-08-22 15:29:16 +08:00

65 lines
1.6 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 ExampleJson_Set_j() {
j := gjson.New(nil)
j.Set("name", "John")
j.Set("score", 99.5)
fmt.Printf(
"Name: %s, Score: %v\n",
j.Get("name").String(),
j.Get("score").Float32(),
)
fmt.Println(j.MustToJsonString())
// Output:
// Name: John, Score: 99.5
// {"name":"John","score":99.5}
}
func ExampleJson_Set_sprintf() {
j := gjson.New(nil)
for i := 0; i < 5; i++ {
j.Set(fmt.Sprintf(`%d.id`, i), i)
j.Set(fmt.Sprintf(`%d.name`, i), fmt.Sprintf(`student-%d`, i))
}
fmt.Println(j.MustToJsonString())
// Output:
// [{"id":0,"name":"student-0"},{"id":1,"name":"student-1"},{"id":2,"name":"student-2"},{"id":3,"name":"student-3"},{"id":4,"name":"student-4"}]
}
func ExampleJson_Set_data() {
data :=
`{
"users" : {
"count" : 2,
"list" : [
{"name" : "Ming", "score" : 60},
{"name" : "John", "score" : 59}
]
}
}`
if j, err := gjson.DecodeToJson(data); err != nil {
panic(err)
} else {
j.Set("users.list.1.score", 100)
fmt.Println("John Score:", j.Get("users.list.1.score").Float32())
fmt.Println(j.MustToJsonString())
}
// Output:
// John Score: 100
// {"users":{"count":2,"list":[{"name":"Ming","score":60},{"name":"John","score":100}]}}
}