From 28837977f27b68ff6c484994051814a3b8fdbe29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Tue, 13 Apr 2021 23:43:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E7=89=88=E6=8F=90=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E6=8D=AE,=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=9C=AA=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json/build.go | 47 ++++++++++++++++------------------------------- json/json_test.go | 6 +++++- json/parse.go | 26 ++++++++++++++++---------- 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/json/build.go b/json/build.go index 0d9260f..a2af63e 100644 --- a/json/build.go +++ b/json/build.go @@ -37,6 +37,7 @@ type JSONode struct { IsIndexNode bool // 是否是slice的索引节点 Sort int // 此属性用于 slice解析,保证最终排序是对的 IsComplex bool // 是否为复杂数据类型 + IsString bool // 是否为字符串 } // NewDynamicJSON 获取JSON实例 @@ -129,24 +130,13 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int *tplList = append(*tplList, startSymbol) } else { *tplList = append(*tplList, valFormat) - switch val := root.Value.(type) { - case string: - *valList = append(*valList, val) - default: - *valList = append(*valList, root.Value) - } + *valList = append(*valList, root.Value) return tplList, valList } } else { if len(root.Child) == 0 { - switch val := root.Value.(type) { - case string: - *valList = append(*valList, val) - *tplList = append(*tplList, dj.getValFormat(root)) - default: - *tplList = append(*tplList, dj.getValFormat(root)) - *valList = append(*valList, root.Value) - } + *tplList = append(*tplList, valFormat) + *valList = append(*valList, root.Value) } else { *tplList = append(*tplList, startSymbol) } @@ -171,6 +161,7 @@ func (dj *DynamicJSON) getValFormat(root *JSONode) string { // 还有自节点的情况下,不需要占位符 return "" } + if root.IsHasLastBrother { return key + ":%v," } @@ -181,24 +172,11 @@ func (dj *DynamicJSON) getValFormat(root *JSONode) string { // 是list的索引节点,且有子节点 return "" } - switch root.Value.(type) { - case string: - if !root.IsComplex { - if root.IsHasLastBrother { - return "\"%v\"," - } - return "\"%v\"" - } - if root.IsHasLastBrother { - return "%v," - } - return "%v" - default: - if root.IsHasLastBrother { - return "%v," - } - return "%v" + if root.IsHasLastBrother { + return "%v," } + return "%v" + } // getStartSymbol 计算起始的符号 @@ -311,6 +289,13 @@ func (dj *DynamicJSON) createNode(parent *JSONode, key string, value interface{} IsRoot: false, IsHasLastBrother: false, IsComplex: isComplexType, + IsString: false, + } + if !isComplexType { + switch value.(type) { + case string: + newNode.IsString = true + } } parent.IsSlice, newNode.Sort = dj.extraSliceIndex(key) if parent.IsSlice { diff --git a/json/json_test.go b/json/json_test.go index 7e4e4b0..7b1c394 100644 --- a/json/json_test.go +++ b/json/json_test.go @@ -64,8 +64,12 @@ func TestSelect(t *testing.T) { }, "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}, + {"name": "bob", "age": 28, "number": 2}, + }, } - pathList := []string{"name", "extra.age", "slice", "map"} + pathList := []string{"name", "extra.age", "slice", "map", "table.[].name|number|test"} r, e := NewParseJSONTree(source).Parse(pathList) fmt.Println(r.String(), e) } diff --git a/json/parse.go b/json/parse.go index bf33033..bd60f4c 100644 --- a/json/parse.go +++ b/json/parse.go @@ -52,17 +52,23 @@ func (pjt *ParseJSONTree) Parse(pathList []string) (*DynamicJSON, error) { source := string(byteData) for _, path := range pathList { // 为数组的处理 - pathArr := strings.Split(path, "[]") - for idx, item := range pathArr { - val := gjson.Get(source, item) - isComplexType := val.IsArray() - if len(pathArr)-1 == idx { - // 当前是最后一项,说明不是数组 - if isComplexType { - fmt.Println("这是一个数组") + pathArr := strings.Split(path, ".[].") + val := gjson.Get(source, pathArr[0]) + isComplexType := val.IsArray() || val.IsObject() + if len(pathArr) == 1 { + result.SetValue(pathArr[0], val.Raw, isComplexType) + continue + } + // 支持list再抽取一层,处于性能考虑,这么限制,不做递归无限深度处理 + // 还能继续抽取,就认为是 []map[string]interface{} + ketList := strings.Split(pathArr[1], "|") + for idx, item := range val.Array() { + data := item.Map() + for _, key := range ketList { + if v, exist := data[key]; exist { + result.SetValue(fmt.Sprintf(pathArr[0]+".[%d]."+key, idx), data[key].Raw, v.IsObject() || v.IsArray()) } - result.SetValue(item, val.Raw, isComplexType) - continue + // 结果集中不存在对应key,丢弃 } } }