54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
|
// Package json_tool ...
|
||
|
//
|
||
|
// Description : json_tool ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<张德满>
|
||
|
//
|
||
|
// Date : 2022-01-09 10:48 PM
|
||
|
package json_tool
|
||
|
|
||
|
import (
|
||
|
"github.com/tidwall/gjson"
|
||
|
)
|
||
|
|
||
|
// GetJSONDataStruct 获取JSON数据的结构
|
||
|
//
|
||
|
// Author : go_developer@163.com<张德满>
|
||
|
//
|
||
|
// Date : 10:53 PM 2022/1/9
|
||
|
func GetJSONDataStruct(data string) []string {
|
||
|
pathList := make([]string, 0)
|
||
|
r := gjson.Parse(data)
|
||
|
r.ForEach(func(key, value gjson.Result) bool {
|
||
|
if value.IsObject() {
|
||
|
list := GetJSONDataStruct(value.String())
|
||
|
for _, k := range list {
|
||
|
pathList = append(pathList, key.String()+"."+k)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if value.IsArray() {
|
||
|
dataList := value.Array()
|
||
|
if len(dataList) > 0 {
|
||
|
if !dataList[0].IsObject() && !dataList[0].IsArray() {
|
||
|
pathList = append(pathList, key.String())
|
||
|
} else {
|
||
|
list := GetJSONDataStruct(dataList[0].String())
|
||
|
for _, k := range list {
|
||
|
pathList = append(pathList, key.String()+".[]."+k)
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
pathList = append(pathList, key.String())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if !value.IsObject() && !value.IsArray() {
|
||
|
pathList = append(pathList, key.String())
|
||
|
}
|
||
|
|
||
|
return true
|
||
|
})
|
||
|
return pathList
|
||
|
}
|