Compare commits

...
4 Commits
Author SHA1 Message Date
maguodong b10df1f064 refactor(common):优化JSON清理逻辑
- 使用gjson.NewWithOptions替代gjson.DecodeToJson解析JSON字符串- 添加日志记录清理过程中的JSON数据
- 修改cleanData函数参数类型为interface{}
- 更新错误处理逻辑并添加错误日志
- 调整JSON编码方式,使用ToJson()方法返回结果
- 移除不必要的类型转换和中间变量声明
2025-11-12 15:05:04 +08:00
gaoda aab78082ce refactor(common):优化JSON清理逻辑
- 使用gjson.DecodeToJson替代gjson.Unmarshal提高解析效率
- 添加StrNumber选项支持字符串数字处理
- 简化数据清理流程
- 移除不必要的中间变量声明
- 提升JSON解析的准确性和性能
2025-11-12 10:58:30 +08:00
gaoda 59158d7309 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	lklsdk/common/json_cleaner.go
2025-11-12 10:51:41 +08:00
gaoda 03aec086bf refactor(common): 替换JSON处理库为gjson
-encoding 将标准库/json替换为github.com/gogf/gf/v2/encoding/gjson
- 更新Unmarshal方法调用为gjson.Unmarshal- 更新Marshal方法调用为gjson.Marshal- 保持原有数据清理逻辑不变
2025-11-12 09:55:46 +08:00
+10 -13
View File
@@ -4,27 +4,24 @@ import (
"reflect"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/glog"
)
// CleanJSON 清理JSON字符串中的空值和0值字段
func CleanJSON(jsonStr string) ([]byte, error) {
var data interface{}
// 解析JSON字符串
if err := gjson.Unmarshal([]byte(jsonStr), &data); err != nil {
return nil, err
}
// 解析为JSON
//var data interface{}
json := gjson.NewWithOptions(jsonStr, gjson.Options{StrNumber: true})
glog.Infof(gctx.New(), "Cleaning JSON: %+v", json)
// 递归清理数据
cleaned := cleanData(data)
// 转换回JSON字符串
result, err := gjson.Marshal(cleaned)
cleaned := cleanData(json.Interface())
jsons, err := gjson.DecodeToJson(cleaned, gjson.Options{StrNumber: true})
if err != nil {
glog.Errorf(gctx.New(), "Error decoding JSON: %v", err)
return nil, err
}
return result, nil
return jsons.ToJson()
}
// cleanData 递归清理map或slice中的空值和0值