merge feature/json

This commit is contained in:
白茶清欢 2022-01-10 14:30:26 +08:00
commit 186f103fa5

View File

@ -8,6 +8,7 @@
package json_tool
import (
"github.com/pkg/errors"
"github.com/tidwall/gjson"
)
@ -16,12 +17,15 @@ import (
// Author : go_developer@163.com<张德满>
//
// Date : 10:53 PM 2022/1/9
func GetJSONDataStruct(data string) []string {
func GetJSONDataStruct(data string) ([]string, error) {
if !gjson.Valid(data) {
return make([]string, 0), errors.New("JSON format is invalid")
}
pathList := make([]string, 0)
r := gjson.Parse(data)
r.ForEach(func(key, value gjson.Result) bool {
if value.IsObject() {
list := GetJSONDataStruct(value.String())
list, _ := GetJSONDataStruct(value.String())
for _, k := range list {
pathList = append(pathList, key.String()+"."+k)
}
@ -33,7 +37,7 @@ func GetJSONDataStruct(data string) []string {
if !dataList[0].IsObject() && !dataList[0].IsArray() {
pathList = append(pathList, key.String())
} else {
list := GetJSONDataStruct(dataList[0].String())
list, _ := GetJSONDataStruct(dataList[0].String())
for _, k := range list {
pathList = append(pathList, key.String()+".[]."+k)
}
@ -49,5 +53,5 @@ func GetJSONDataStruct(data string) []string {
return true
})
return pathList
return pathList, nil
}