完成一个简版JSON树构造

This commit is contained in:
2021-03-11 00:05:56 +08:00
parent dc943d4549
commit 1f425c3973
6 changed files with 238 additions and 7 deletions

View File

@ -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
View 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)
}