解决输入数据即为数组, 结构解析错误问题

This commit is contained in:
白茶清欢 2023-08-27 00:22:42 +08:00
parent 4bf4d84db4
commit 78b3ae43e5

View File

@ -117,7 +117,11 @@ func GetJSONDataStructWithType(data string) ([]Field, error) {
}
pathList := make([]Field, 0)
r := gjson.Parse(data)
inputIsArr := r.IsArray()
r.ForEach(func(key, value gjson.Result) bool {
if inputIsArr && key.Int() > 0 {
return true
}
if value.Value() == nil {
pathList = append(pathList, Field{
Path: key.String(),
@ -183,6 +187,11 @@ func GetJSONDataStructWithType(data string) ([]Field, error) {
return true
})
for idx := 0; idx < len(pathList); idx++ {
if inputIsArr && strings.HasPrefix(pathList[idx].Path, "0.") {
pathList[idx].Path = strings.Replace(pathList[idx].Path, "0.", "[].", 1)
}
}
return pathList, nil
}