增加递归抽取不同层级的array
This commit is contained in:
parent
3260dcbaf9
commit
3047c60428
@ -8,10 +8,12 @@
|
||||
package json_tool
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
@ -80,7 +82,7 @@ func (df *DataFilter) Filter() (string, error) {
|
||||
sourcePathArr := strings.Split(item.SourceKey, ".[].")
|
||||
mapPathArr := strings.Split(item.MapKey, ".[].")
|
||||
if len(sourcePathArr) != len(mapPathArr) {
|
||||
return "", errors.New("slice转化原始数据深度与目标数据深度不一致")
|
||||
// return "", errors.New("slice转化原始数据深度与目标数据深度不一致")
|
||||
}
|
||||
continue
|
||||
}
|
||||
@ -177,7 +179,7 @@ func (df *DataFilter) formatRule() {
|
||||
continue
|
||||
}
|
||||
// 数组层级深度不同,重新对对齐数据
|
||||
diffArr := sourcePathArr[0: len(sourcePathArr) - len(mapPathArr)]
|
||||
diffArr := sourcePathArr[0 : len(sourcePathArr)-len(mapPathArr)-1]
|
||||
df.dealDiffArr(diffArr)
|
||||
}
|
||||
} else {
|
||||
@ -210,13 +212,39 @@ func (df *DataFilter) dealDiffArr(diffArr []string) {
|
||||
// 已经处理过, 不再重复处理
|
||||
return
|
||||
}
|
||||
JSONObject, _ := gabs.Consume(df.source)
|
||||
newPath := util.GenRandomString("", 8)
|
||||
_, _ = JSONObject.ArrayCount()
|
||||
newDataList := gabs.New()
|
||||
// 没处理过, 开始处理
|
||||
for index, path := range diffArr {
|
||||
if len(diffArr) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// 没处理过, 开始处理
|
||||
jsonResultList := df.getArrayData(df.source, diffArr)
|
||||
fmt.Println(jsonResultList)
|
||||
if len(jsonResultList) == 0 {
|
||||
return
|
||||
}
|
||||
JSONObject.SetP(newPath, newDataList.String())
|
||||
newDataList := gabs.New()
|
||||
newPath := util.GenRandomString("", 8)
|
||||
for _, item := range jsonResultList {
|
||||
newDataList.ArrayAppend(item.String())
|
||||
}
|
||||
JSONObject, _ := gabs.Consume(df.source)
|
||||
_, _ = JSONObject.SetP(newDataList.Data(), newPath)
|
||||
fmt.Println(123, JSONObject.String())
|
||||
}
|
||||
|
||||
// getArrayData 获取数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2:22 下午 2022/1/26
|
||||
func (df *DataFilter) getArrayData(source string, pathArr []string) []gjson.Result {
|
||||
if len(pathArr) == 1 {
|
||||
return gjson.Get(source, pathArr[0]).Array()
|
||||
}
|
||||
resultList := make([]gjson.Result, 0)
|
||||
dataList := gjson.Get(source, pathArr[0]).Array()
|
||||
for idx := 0; idx < len(dataList); idx++ {
|
||||
resultList = append(resultList, df.getArrayData(gjson.Get(source, dataList[idx].String()).String(), pathArr[1:])...)
|
||||
}
|
||||
return resultList
|
||||
}
|
||||
|
@ -240,3 +240,37 @@ func TestDataFilterForObiToSlice(t *testing.T) {
|
||||
filter.UserItemToSlice()
|
||||
fmt.Println(filter.Filter())
|
||||
}
|
||||
|
||||
// TestDataFilterDiffArr ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:27 下午 2022/1/26
|
||||
func TestDataFilterDiffArr(t *testing.T) {
|
||||
source := map[string]interface{}{
|
||||
"name": "zhangdeman",
|
||||
"age": 18,
|
||||
"height": 180,
|
||||
"extra": map[string]interface{}{
|
||||
"age": 18,
|
||||
"height": 180,
|
||||
"slice": []int{1, 2, 3},
|
||||
},
|
||||
"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}}},
|
||||
},
|
||||
}
|
||||
rule := []*FilterDataRule{
|
||||
// {SourceKey: "name", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"},
|
||||
{SourceKey: "table.[].user_list.[].name", MapKey: "user_list.[].detail.name", DefaultValue: "用户姓名默认值"},
|
||||
{SourceKey: "table.[].user_list.[].age", MapKey: "user_list.[]detail.age", DefaultValue: "用户姓名默认值"},
|
||||
}
|
||||
byteData, _ := json.Marshal(source)
|
||||
filter := NewDataFilter(string(byteData), rule)
|
||||
filter.UserItemToSlice()
|
||||
fmt.Println(filter.Filter())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user