升级提取指定深度的数据最终字段作为list

This commit is contained in:
2023-09-01 23:17:04 +08:00
parent bae9380210
commit e79ac95858
4 changed files with 132 additions and 36 deletions

View File

@ -8,9 +8,11 @@
package json_tool
import (
"encoding/json"
"fmt"
"git.zhangdeman.cn/zhangdeman/serialize"
"testing"
"git.zhangdeman.cn/zhangdeman/serialize"
)
// TestDataFilter_FilterNormalData 最基础对象
@ -41,3 +43,45 @@ func TestDataFilter_FilterNormalData(t *testing.T) {
}
fmt.Println(df.Filter())
}
// TestDataFilter_getDataAsSlice 测试循环读取数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:21 2023/9/1
func TestDataFilter_getDataAsSlice(t *testing.T) {
data := map[string]interface{}{
"list": []interface{}{
map[string]interface{}{
"name": "1",
"age": "2",
"list_test": []interface{}{
map[string]interface{}{
"a": "a",
"b": "b",
},
},
},
map[string]interface{}{
"name": "3",
"age": "4",
"list_test": []interface{}{
map[string]interface{}{
"a": "a1",
"b": "b1",
},
},
},
map[string]interface{}{
"name": "5",
"age": "6",
},
},
}
byteData, _ := json.Marshal(data)
df := &DataFilter{}
fmt.Println(df.getDataAsSlice(string(byteData), []string{"list", "name"}))
fmt.Println(df.getDataAsSlice(string(byteData), []string{"list", "age"}))
fmt.Println(df.getDataAsSlice(string(byteData), []string{"list", "age", "list_test", "a"}))
fmt.Println(df.getDataAsSlice(string(byteData), []string{"list", "age", "list_test", "b"}))
}