第一版提取数据,异常处理未完成

This commit is contained in:
白茶清欢 2021-04-13 23:43:04 +08:00
parent d98822a5ee
commit 28837977f2
3 changed files with 37 additions and 42 deletions

View File

@ -37,6 +37,7 @@ type JSONode struct {
IsIndexNode bool // 是否是slice的索引节点
Sort int // 此属性用于 slice解析,保证最终排序是对的
IsComplex bool // 是否为复杂数据类型
IsString bool // 是否为字符串
}
// NewDynamicJSON 获取JSON实例
@ -129,24 +130,13 @@ func (dj *DynamicJSON) buildTpl(root *JSONode, tplList *[]string, valList *[]int
*tplList = append(*tplList, startSymbol)
} else {
*tplList = append(*tplList, valFormat)
switch val := root.Value.(type) {
case string:
*valList = append(*valList, val)
default:
*valList = append(*valList, root.Value)
}
return tplList, valList
}
} else {
if len(root.Child) == 0 {
switch val := root.Value.(type) {
case string:
*valList = append(*valList, val)
*tplList = append(*tplList, dj.getValFormat(root))
default:
*tplList = append(*tplList, dj.getValFormat(root))
*tplList = append(*tplList, valFormat)
*valList = append(*valList, root.Value)
}
} else {
*tplList = append(*tplList, startSymbol)
}
@ -171,6 +161,7 @@ func (dj *DynamicJSON) getValFormat(root *JSONode) string {
// 还有自节点的情况下,不需要占位符
return ""
}
if root.IsHasLastBrother {
return key + ":%v,"
}
@ -181,24 +172,11 @@ func (dj *DynamicJSON) getValFormat(root *JSONode) string {
// 是list的索引节点,且有子节点
return ""
}
switch root.Value.(type) {
case string:
if !root.IsComplex {
if root.IsHasLastBrother {
return "\"%v\","
}
return "\"%v\""
}
if root.IsHasLastBrother {
return "%v,"
}
return "%v"
default:
if root.IsHasLastBrother {
return "%v,"
}
return "%v"
}
}
// getStartSymbol 计算起始的符号
@ -311,6 +289,13 @@ func (dj *DynamicJSON) createNode(parent *JSONode, key string, value interface{}
IsRoot: false,
IsHasLastBrother: false,
IsComplex: isComplexType,
IsString: false,
}
if !isComplexType {
switch value.(type) {
case string:
newNode.IsString = true
}
}
parent.IsSlice, newNode.Sort = dj.extraSliceIndex(key)
if parent.IsSlice {

View File

@ -64,8 +64,12 @@ func TestSelect(t *testing.T) {
},
"slice": []int{1, 2, 3},
"map": map[string]interface{}{"a": 1, "b": 2, "c": 4},
"table": []map[string]interface{}{
{"name": "alex", "age": 18, "number": 1},
{"name": "bob", "age": 28, "number": 2},
},
}
pathList := []string{"name", "extra.age", "slice", "map"}
pathList := []string{"name", "extra.age", "slice", "map", "table.[].name|number|test"}
r, e := NewParseJSONTree(source).Parse(pathList)
fmt.Println(r.String(), e)
}

View File

@ -52,18 +52,24 @@ func (pjt *ParseJSONTree) Parse(pathList []string) (*DynamicJSON, error) {
source := string(byteData)
for _, path := range pathList {
// 为数组的处理
pathArr := strings.Split(path, "[]")
for idx, item := range pathArr {
val := gjson.Get(source, item)
isComplexType := val.IsArray()
if len(pathArr)-1 == idx {
// 当前是最后一项,说明不是数组
if isComplexType {
fmt.Println("这是一个数组")
}
result.SetValue(item, val.Raw, isComplexType)
pathArr := strings.Split(path, ".[].")
val := gjson.Get(source, pathArr[0])
isComplexType := val.IsArray() || val.IsObject()
if len(pathArr) == 1 {
result.SetValue(pathArr[0], val.Raw, isComplexType)
continue
}
// 支持list再抽取一层,处于性能考虑,这么限制,不做递归无限深度处理
// 还能继续抽取,就认为是 []map[string]interface{}
ketList := strings.Split(pathArr[1], "|")
for idx, item := range val.Array() {
data := item.Map()
for _, key := range ketList {
if v, exist := data[key]; exist {
result.SetValue(fmt.Sprintf(pathArr[0]+".[%d]."+key, idx), data[key].Raw, v.IsObject() || v.IsArray())
}
// 结果集中不存在对应key,丢弃
}
}
}
return result, nil