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

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

View File

@ -64,8 +64,12 @@ func TestSelect(t *testing.T) {
}, },
"slice": []int{1, 2, 3}, "slice": []int{1, 2, 3},
"map": map[string]interface{}{"a": 1, "b": 2, "c": 4}, "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) r, e := NewParseJSONTree(source).Parse(pathList)
fmt.Println(r.String(), e) fmt.Println(r.String(), e)
} }

View File

@ -52,17 +52,23 @@ func (pjt *ParseJSONTree) Parse(pathList []string) (*DynamicJSON, error) {
source := string(byteData) source := string(byteData)
for _, path := range pathList { for _, path := range pathList {
// 为数组的处理 // 为数组的处理
pathArr := strings.Split(path, "[]") pathArr := strings.Split(path, ".[].")
for idx, item := range pathArr { val := gjson.Get(source, pathArr[0])
val := gjson.Get(source, item) isComplexType := val.IsArray() || val.IsObject()
isComplexType := val.IsArray() if len(pathArr) == 1 {
if len(pathArr)-1 == idx { result.SetValue(pathArr[0], val.Raw, isComplexType)
// 当前是最后一项,说明不是数组 continue
if isComplexType { }
fmt.Println("这是一个数组") // 支持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())
} }
result.SetValue(item, val.Raw, isComplexType) // 结果集中不存在对应key,丢弃
continue
} }
} }
} }