完成一个简版JSON树构造
This commit is contained in:
@ -24,7 +24,7 @@ func Filter(source []byte, filter []string) ([]byte, error) {
|
||||
setErr error
|
||||
)
|
||||
for _, item := range filter {
|
||||
fieldList := strings.Split(item, ",")
|
||||
fieldList := strings.Split(item, ".")
|
||||
val, _, _, err := jsonparser.Get(source, fieldList...)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
|
42
safe/data_test.go
Normal file
42
safe/data_test.go
Normal file
@ -0,0 +1,42 @@
|
||||
// Package safe...
|
||||
//
|
||||
// Description : safe...
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 2021-03-10 10:07 下午
|
||||
package safe
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestFilter(t *testing.T) {
|
||||
source := []byte(`
|
||||
{
|
||||
"name":"zhangdeman",
|
||||
"age":18,
|
||||
"extra":{"height":180, "company":"com"}
|
||||
}
|
||||
`)
|
||||
result, err := Filter(source, []string{"name", "extra.company"})
|
||||
if nil != err {
|
||||
fmt.Println(err.Error())
|
||||
} else {
|
||||
fmt.Println(string(result))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGJSON(t *testing.T) {
|
||||
source := `
|
||||
{
|
||||
"name":"zhangdeman",
|
||||
"age":18,
|
||||
"extra":{"height":180, "company":"com"}
|
||||
}
|
||||
`
|
||||
fmt.Println(gjson.Get(source, "extra.height").Raw)
|
||||
}
|
Reference in New Issue
Block a user