From 16156214ed829f8e598daba9bd9f1fab234d71c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Sun, 14 Mar 2021 23:36:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9E=84=E5=BB=BA=E4=B8=80?= =?UTF-8?q?=E4=B8=AAslice=E8=BE=93=E5=87=BA=E6=95=B0=E6=8D=AE=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json/build.go | 9 ++++++++- json/json_test.go | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/json/build.go b/json/build.go index 469b8c5..467aad4 100644 --- a/json/build.go +++ b/json/build.go @@ -200,7 +200,14 @@ func (dj *DynamicJSON) getValFormat(root *JSONode) string { // // Date : 12:21 下午 2021/3/13 func (dj *DynamicJSON) getStartSymbol(root *JSONode) string { - if nil == root || root.IsRoot { + if nil == root { + return "{" + } + + if root.IsRoot { + if root.IsSlice { + return "[" + } return "{" } key := fmt.Sprintf("\"%s\"", root.Key) diff --git a/json/json_test.go b/json/json_test.go index e99b9f5..4858152 100644 --- a/json/json_test.go +++ b/json/json_test.go @@ -33,6 +33,11 @@ func TestJSON(t *testing.T) { tree.SetValue("slice.[2].name", "man") tree.SetValue("slice.[3]", "zhangdeman") fmt.Println(tree.String()) + tree = NewDynamicJSON() + tree.SetValue("[0]", "zhang") + tree.SetValue("[1]", "de") + tree.SetValue("[2]", "man") + fmt.Println(tree.String()) } // TestType 判断数据类型断言