From df00e136026c0fb51b2050567e31e85297b921e2 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:28:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=9D=9E=E6=95=B4=E9=BD=90?= =?UTF-8?q?=E7=9A=84list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json/build.go | 49 +++++++++++++++++++++++++++++++++++++++-------- json/json_test.go | 1 + 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/json/build.go b/json/build.go index 8705a7c..b997fa9 100644 --- a/json/build.go +++ b/json/build.go @@ -149,21 +149,54 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int } } else { - *tplList = append(*tplList, "{") + if len(root.Child) == 0 { + + switch val := root.Value.(type) { + case string: + *valList = append(*valList, val) + if root.IsHasLastBrother { + *tplList = append(*tplList, "\"%v\",") + } else { + *tplList = append(*tplList, "\"%v\"") + + } + default: + if root.IsHasLastBrother { + *tplList = append(*tplList, "%v,") + } else { + *tplList = append(*tplList, "%v") + + } + *valList = append(*valList, root.Value) + } + } else { + *tplList = append(*tplList, "{") + } + } for _, node := range root.Child { dj.buildTpl(node, tplList, valList) } - if root.IsHasLastBrother { - *tplList = append(*tplList, "},") - } else { - if root.IsSlice { - *tplList = append(*tplList, "]") + if !root.IsIndexNode { + if root.IsHasLastBrother { + *tplList = append(*tplList, "},") } else { - *tplList = append(*tplList, "}") - + if root.IsSlice { + *tplList = append(*tplList, "]") + } else { + *tplList = append(*tplList, "}") + } + } + } else { + if len(root.Child) > 0 { + if root.IsHasLastBrother { + *tplList = append(*tplList, "},") + } else { + *tplList = append(*tplList, "}") + } } } + return tplList, valList } diff --git a/json/json_test.go b/json/json_test.go index bc71618..dafec96 100644 --- a/json/json_test.go +++ b/json/json_test.go @@ -26,5 +26,6 @@ func TestJSON(t *testing.T) { tree.SetValue("slice.[0].name", "zhang") tree.SetValue("slice.[1].name", "de") tree.SetValue("slice.[2].name", "man") + tree.SetValue("slice.[3]", "zhangdeman") fmt.Println(tree.String()) }