From 3c6c8067f836c491e684f07aae5e8face3c62bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Fri, 12 Mar 2021 23:08:48 +0800 Subject: [PATCH] =?UTF-8?q?json=E6=94=AF=E6=8C=81=E6=A0=87=E5=87=86?= =?UTF-8?q?=E7=9A=84list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json/build.go | 63 +++++++++++++++++++++++++++++------------------ json/json_test.go | 3 +++ 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/json/build.go b/json/build.go index fc97fcb..ed6092b 100644 --- a/json/build.go +++ b/json/build.go @@ -34,6 +34,7 @@ type JSONode struct { IsRoot bool // 是否根节点 IsHasLastBrother bool // 在此之后是否有其他兄弟节点 IsSlice bool // 是否是list + IsIndexNode bool // 是否是slice的索引节点 Sort int // 此属性用于 slice解析,保证最终排序是对的 } @@ -89,8 +90,10 @@ func (dj *DynamicJSON) SetValue(path string, value interface{}) { val = value } _ = dj.createNode(parent, key, val) - searchRoot = parent.Child[len(parent.Child)-1] - parent = parent.Child[len(parent.Child)-1] + if len(parent.Child) > 0 { + searchRoot = parent.Child[len(parent.Child)-1] + parent = parent.Child[len(parent.Child)-1] + } } } } @@ -117,30 +120,36 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int *tplList = append(*tplList, "}") return tplList, valList } + key := "\"" + root.Key + "\"" - if len(root.Child) > 0 { - if root.IsRoot { - *tplList = append(*tplList, "{") - } else { - if root.IsSlice { - *tplList = append(*tplList, key+":{") + if !root.IsIndexNode { + if len(root.Child) > 0 { + if root.IsRoot { + *tplList = append(*tplList, "{") } else { - *tplList = append(*tplList, key+":{") + if root.IsSlice { + *tplList = append(*tplList, key+":[") + } else { + *tplList = append(*tplList, key+":{") + } } + } else { + if root.IsHasLastBrother { + *tplList = append(*tplList, key+":%v,") + } else { + *tplList = append(*tplList, key+":%v") + } + switch val := root.Value.(type) { + case string: + *valList = append(*valList, "\""+val+"\"") + default: + *valList = append(*valList, root.Value) + } + return tplList, valList + } } else { - if root.IsHasLastBrother { - *tplList = append(*tplList, key+":%v,") - } else { - *tplList = append(*tplList, key+":%v") - } - switch val := root.Value.(type) { - case string: - *valList = append(*valList, "\""+val+"\"") - default: - *valList = append(*valList, root.Value) - } - return tplList, valList + *tplList = append(*tplList, "{") } for _, node := range root.Child { dj.buildTpl(node, tplList, valList) @@ -149,7 +158,7 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int *tplList = append(*tplList, "},") } else { if root.IsSlice { - *tplList = append(*tplList, "}]") + *tplList = append(*tplList, "]") } else { *tplList = append(*tplList, "}") @@ -195,13 +204,19 @@ func (dj *DynamicJSON) createNode(parent *JSONode, key string, value interface{} // 存在子节点,设置当前子节点还有其他兄弟节点 parent.Child[len(parent.Child)-1].IsHasLastBrother = true } - parent.Child = append(parent.Child, &JSONode{ + + newNode := &JSONode{ Key: key, Value: value, Child: make([]*JSONode, 0), IsRoot: false, IsHasLastBrother: false, - }) + } + parent.IsSlice, newNode.Sort = dj.extraSliceIndex(key) + if parent.IsSlice { + newNode.IsIndexNode = true + } + parent.Child = append(parent.Child, newNode) dj.nodeCnt++ _ = dj.lock.Unlock("") return nil diff --git a/json/json_test.go b/json/json_test.go index fcaa143..bc71618 100644 --- a/json/json_test.go +++ b/json/json_test.go @@ -23,5 +23,8 @@ func TestJSON(t *testing.T) { tree.SetValue("work", "111") tree.SetValue("good.price", "180") tree.SetValue("good.unit", "$") + tree.SetValue("slice.[0].name", "zhang") + tree.SetValue("slice.[1].name", "de") + tree.SetValue("slice.[2].name", "man") fmt.Println(tree.String()) }