Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aab78082ce | ||
|
|
59158d7309 | ||
|
|
6f685ea087 | ||
|
|
b038c64873 | ||
|
|
03aec086bf |
@@ -1,24 +1,24 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
)
|
||||
|
||||
// CleanJSON 清理JSON字符串中的空值和0值字段
|
||||
func CleanJSON(jsonStr string) ([]byte, error) {
|
||||
var data interface{}
|
||||
|
||||
// 解析JSON字符串
|
||||
if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
|
||||
// 解析为JSON
|
||||
json, err := gjson.DecodeToJson(jsonStr, gjson.Options{StrNumber: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 递归清理数据
|
||||
cleaned := cleanData(data)
|
||||
cleaned := cleanData(json)
|
||||
|
||||
// 转换回JSON字符串
|
||||
result, err := json.Marshal(cleaned)
|
||||
result, err := gjson.Marshal(cleaned)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user