Files
gf/encoding/gjson/gjson_z_example_pattern_test.go

72 lines
1.5 KiB
Go
Raw Normal View History

2020-03-21 21:32:02 +08:00
// Copyright 2017 gf Author(https://github.com/gogf/gf). 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/encoding/gjson"
)
2020-03-22 12:49:46 +08:00
func Example_patternGet() {
2020-03-21 21:32:02 +08:00
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 {
fmt.Println("John Score:", j.GetFloat32("users.list.1.score"))
}
// Output:
// John Score: 99.5
}
2020-03-22 12:49:46 +08:00
func Example_patternCustomSplitChar() {
2020-03-21 21:32:02 +08:00
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.GetFloat32("users#list#1#score"))
}
// Output:
// John Score: 99.5
}
2020-03-22 12:49:46 +08:00
func Example_patternViolenceCheck() {
2020-03-21 21:32:02 +08:00
data :=
`{
"users" : {
"count" : 100
},
"users.count" : 101
}`
if j, err := gjson.DecodeToJson(data); err != nil {
panic(err)
} else {
j.SetViolenceCheck(true)
fmt.Println("Users Count:", j.GetInt("users.count"))
}
// Output:
// Users Count: 101
}