Compare commits
No commits in common. "1cae7173d639a46182d0ef03103fd4485ebe3d51" and "daae6a16d4f0123567d0fd8aab551466e9443913" have entirely different histories.
1cae7173d6
...
daae6a16d4
@ -20,7 +20,7 @@ import (
|
||||
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
||||
)
|
||||
|
||||
// NewFilter 获取解析的实例
|
||||
// NewParseJSONTree 获取解析的实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
@ -32,7 +32,7 @@ func NewFilter(data interface{}, rule map[string]string) *Filter {
|
||||
}
|
||||
}
|
||||
|
||||
// Filter 解析json树
|
||||
// ParseJSONTree 解析json树
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
|
@ -8,7 +8,6 @@
|
||||
package json_tool
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
@ -85,43 +84,3 @@ func TestSelect(t *testing.T) {
|
||||
}
|
||||
fmt.Println(d.String())
|
||||
}
|
||||
|
||||
// TestParse 测试获取JSON数据结构
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 10:59 PM 2022/1/9
|
||||
func TestParse(t *testing.T) {
|
||||
source := map[string]interface{}{
|
||||
"name": "zhangdeman",
|
||||
"extra": map[string]interface{}{
|
||||
"age": 18,
|
||||
"height": 180,
|
||||
"slice": []int{1, 2, 3},
|
||||
"obj": map[string]interface{}{
|
||||
"la": "aaaa",
|
||||
},
|
||||
},
|
||||
"slice": []int{1, 2, 3},
|
||||
"map": map[string]interface{}{"a": 1, "b": 2, "c": 4},
|
||||
"table": []map[string]interface{}{
|
||||
{"name": "alex", "age": 18, "number": 1, "obj": map[string]interface{}{"enen": "en"}},
|
||||
{"name": "bob", "age": 28, "number": 2},
|
||||
},
|
||||
"two_slice": []map[string]interface{}{
|
||||
{
|
||||
"students": []map[string]interface{}{
|
||||
{
|
||||
"name": "enen",
|
||||
"age": 18,
|
||||
"score": []float64{1, 2, 3, 45},
|
||||
},
|
||||
},
|
||||
"other": []interface{}{"others"},
|
||||
"read_only": 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
byteData, _ := json.Marshal(source)
|
||||
fmt.Println(GetJSONDataStruct(string(byteData)))
|
||||
}
|
||||
|
@ -1,53 +0,0 @@
|
||||
// Package json_tool ...
|
||||
//
|
||||
// Description : json_tool ...
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 2022-01-09 10:48 PM
|
||||
package json_tool
|
||||
|
||||
import (
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
// GetJSONDataStruct 获取JSON数据的结构
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 10:53 PM 2022/1/9
|
||||
func GetJSONDataStruct(data string) []string {
|
||||
pathList := make([]string, 0)
|
||||
r := gjson.Parse(data)
|
||||
r.ForEach(func(key, value gjson.Result) bool {
|
||||
if value.IsObject() {
|
||||
list := GetJSONDataStruct(value.String())
|
||||
for _, k := range list {
|
||||
pathList = append(pathList, key.String()+"."+k)
|
||||
}
|
||||
}
|
||||
|
||||
if value.IsArray() {
|
||||
dataList := value.Array()
|
||||
if len(dataList) > 0 {
|
||||
if !dataList[0].IsObject() && !dataList[0].IsArray() {
|
||||
pathList = append(pathList, key.String())
|
||||
} else {
|
||||
list := GetJSONDataStruct(dataList[0].String())
|
||||
for _, k := range list {
|
||||
pathList = append(pathList, key.String()+".[]."+k)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pathList = append(pathList, key.String())
|
||||
}
|
||||
}
|
||||
|
||||
if !value.IsObject() && !value.IsArray() {
|
||||
pathList = append(pathList, key.String())
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
return pathList
|
||||
}
|
Loading…
Reference in New Issue
Block a user