diff --git a/json_tool/gabs.go b/json_tool/gabs.go index dcad931..71553d5 100644 --- a/json_tool/gabs.go +++ b/json_tool/gabs.go @@ -8,6 +8,7 @@ package json_tool import ( + "encoding/json" "fmt" "reflect" "strings" @@ -218,18 +219,21 @@ func (df *DataFilter) dealDiffArr(diffArr []string) { // 没处理过, 开始处理 jsonResultList := df.getArrayData(df.source, diffArr) - fmt.Println(jsonResultList) if len(jsonResultList) == 0 { return } - newDataList := gabs.New() newPath := util.GenRandomString("", 8) + var result map[string]interface{} + _ = json.Unmarshal([]byte(df.source), &result) + JSONObject, _ := gabs.Consume(result) + _, _ = JSONObject.ArrayP(newPath) for _, item := range jsonResultList { - newDataList.ArrayAppend(item.String()) + if err := JSONObject.ArrayAppendP(item.Value(), newPath); nil != err { + fmt.Println(err.Error()) + } } - JSONObject, _ := gabs.Consume(df.source) - _, _ = JSONObject.SetP(newDataList.Data(), newPath) - fmt.Println(123, JSONObject.String()) + + df.source = JSONObject.String() } // getArrayData 获取数据 diff --git a/json_tool/json_test.go b/json_tool/json_test.go index 0ea49ad..e8cb6ef 100644 --- a/json_tool/json_test.go +++ b/json_tool/json_test.go @@ -259,9 +259,9 @@ func TestDataFilterDiffArr(t *testing.T) { "slice_data": []int{1, 2, 3}, "map": map[string]interface{}{"a": 1, "b": 2, "c": 4}, "table": []map[string]interface{}{ - {"user_list": map[string]interface{}{"name": "alex", "age": 18, "number": 1}}, - {"user_list": map[string]interface{}{"name": "bob", "age": 28, "number": 2}}, - {"user_list": map[string]interface{}{"name": "andy", "age": 28, "number": 2, "list": []int{1, 2, 3}}}, + {"user_list": []interface{}{map[string]interface{}{"name": "alex", "age": 18, "number": 1}}}, + {"user_list": []interface{}{map[string]interface{}{"name": "bob", "age": 28, "number": 2}}}, + {"user_list": []interface{}{map[string]interface{}{"name": "andy", "age": 28, "number": 2, "list": []int{1, 2, 3}}}}, }, } rule := []*FilterDataRule{