diff --git a/json/build.go b/json/build.go index cf67f83..0d9260f 100644 --- a/json/build.go +++ b/json/build.go @@ -36,6 +36,7 @@ type JSONode struct { IsSlice bool // 是否是list IsIndexNode bool // 是否是slice的索引节点 Sort int // 此属性用于 slice解析,保证最终排序是对的 + IsComplex bool // 是否为复杂数据类型 } // NewDynamicJSON 获取JSON实例 @@ -75,7 +76,7 @@ type DynamicJSON struct { // Author : go_developer@163.com<张德满> // // Date : 10:45 下午 2021/3/10 -func (dj *DynamicJSON) SetValue(path string, value interface{}) { +func (dj *DynamicJSON) SetValue(path string, value interface{}, isComplexType bool) { pathList := strings.Split(path, PathSplit) searchRoot := dj.root parent := dj.root @@ -89,7 +90,7 @@ func (dj *DynamicJSON) SetValue(path string, value interface{}) { if keyIndex == len(pathList)-1 { val = value } - _ = dj.createNode(parent, key, val) + _ = dj.createNode(parent, key, val, isComplexType) if len(parent.Child) > 0 { searchRoot = parent.Child[len(parent.Child)-1] parent = parent.Child[len(parent.Child)-1] @@ -130,7 +131,7 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int *tplList = append(*tplList, valFormat) switch val := root.Value.(type) { case string: - *valList = append(*valList, "\""+val+"\"") + *valList = append(*valList, val) default: *valList = append(*valList, root.Value) } @@ -182,10 +183,16 @@ func (dj *DynamicJSON) getValFormat(root *JSONode) string { } switch root.Value.(type) { case string: - if root.IsHasLastBrother { - return "\"%v\"," + if !root.IsComplex { + if root.IsHasLastBrother { + return "\"%v\"," + } + return "\"%v\"" } - return "\"%v\"" + if root.IsHasLastBrother { + return "%v," + } + return "%v" default: if root.IsHasLastBrother { return "%v," @@ -284,7 +291,7 @@ func (dj *DynamicJSON) search(root *JSONode, key string) *JSONode { // Author : go_developer@163.com<张德满> // // Date : 10:57 下午 2021/3/10 -func (dj *DynamicJSON) createNode(parent *JSONode, key string, value interface{}) error { +func (dj *DynamicJSON) createNode(parent *JSONode, key string, value interface{}, isComplexType bool) error { if nil == parent { return errors.New("create node error : parent id nil") } @@ -303,6 +310,7 @@ func (dj *DynamicJSON) createNode(parent *JSONode, key string, value interface{} Child: make([]*JSONode, 0), IsRoot: false, IsHasLastBrother: false, + IsComplex: isComplexType, } parent.IsSlice, newNode.Sort = dj.extraSliceIndex(key) if parent.IsSlice { diff --git a/json/json_test.go b/json/json_test.go index 65d4243..0d1e175 100644 --- a/json/json_test.go +++ b/json/json_test.go @@ -20,23 +20,23 @@ import ( func TestJSON(t *testing.T) { tree := NewDynamicJSON() fmt.Println(tree.extraSliceIndex("[200]")) - tree.SetValue("extra.height.value", 180) - tree.SetValue("extra.height.unit.use", "cm") - tree.SetValue("extra.height.unit.open", "1") - tree.SetValue("name", "zhangdeman") - tree.SetValue("good.name", "good") - 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") - tree.SetValue("slice.[3]", "zhangdeman") + tree.SetValue("extra.height.value", 180, false) + tree.SetValue("extra.height.unit.use", "cm", false) + tree.SetValue("extra.height.unit.open", "1", false) + tree.SetValue("name", "zhangdeman", false) + tree.SetValue("good.name", "good", false) + tree.SetValue("work", "111", false) + tree.SetValue("good.price", "180", false) + tree.SetValue("good.unit", "$", false) + tree.SetValue("slice.[0].name", "zhang", false) + tree.SetValue("slice.[1].name", "de", false) + tree.SetValue("slice.[2].name", "man", false) + tree.SetValue("slice.[3]", "zhangdeman", false) fmt.Println(tree.String()) tree = NewDynamicJSON() - tree.SetValue("[0]", "zhang") - tree.SetValue("[1]", "de") - tree.SetValue("[2]", "man") + tree.SetValue("[0]", "zhang", false) + tree.SetValue("[1]", "de", false) + tree.SetValue("[2]", "man", false) fmt.Println(tree.String()) } diff --git a/json/parse.go b/json/parse.go index 75274ff..db4c0ef 100644 --- a/json/parse.go +++ b/json/parse.go @@ -9,6 +9,7 @@ package json import ( "encoding/json" + "fmt" "reflect" "strings" @@ -53,9 +54,14 @@ func (pjt *ParseJSONTree) Parse(pathList []string) (*DynamicJSON, error) { // 为数组的处理 pathArr := strings.Split(path, "[]") for idx, item := range pathArr { + val := gjson.Get(source, item) + isComplextType := val.IsArray() if len(pathArr)-1 == idx { // 当前是最后一项,说明不是数组 - result.SetValue(item, gjson.Get(source, item).Raw) + if isComplextType { + fmt.Println("这是一个数组") + } + result.SetValue(item, val.Raw, isComplextType) continue } }