From 663a15befc568e7a4de39d6d9538134f51583754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 9 Jan 2022 23:59:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=BE=93=E5=85=A5=E7=9A=84js?= =?UTF-8?q?on=E6=95=B0=E6=8D=AE,=E5=8A=A8=E6=80=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json_tool/filter.go | 4 ++-- json_tool/json_test.go | 41 ++++++++++++++++++++++++++++++++ json_tool/parse.go | 53 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 json_tool/parse.go diff --git a/json_tool/filter.go b/json_tool/filter.go index b418b8c..68d405c 100644 --- a/json_tool/filter.go +++ b/json_tool/filter.go @@ -20,7 +20,7 @@ import ( "git.zhangdeman.cn/zhangdeman/gopkg/util" ) -// NewParseJSONTree 获取解析的实例 +// NewFilter 获取解析的实例 // // Author : go_developer@163.com<白茶清欢> // @@ -32,7 +32,7 @@ func NewFilter(data interface{}, rule map[string]string) *Filter { } } -// ParseJSONTree 解析json树 +// Filter 解析json树 // // Author : go_developer@163.com<白茶清欢> // diff --git a/json_tool/json_test.go b/json_tool/json_test.go index d053fb5..8638c9d 100644 --- a/json_tool/json_test.go +++ b/json_tool/json_test.go @@ -8,6 +8,7 @@ package json_tool import ( + "encoding/json" "fmt" "testing" ) @@ -84,3 +85,43 @@ 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))) +} diff --git a/json_tool/parse.go b/json_tool/parse.go new file mode 100644 index 0000000..04c110e --- /dev/null +++ b/json_tool/parse.go @@ -0,0 +1,53 @@ +// 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 +} -- 2.36.6