From b10df1f064eaaad5f42f627f86a6e1a3321b8ecf Mon Sep 17 00:00:00 2001 From: maguodong Date: Wed, 12 Nov 2025 15:05:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor(common):=E4=BC=98=E5=8C=96JSON?= =?UTF-8?q?=E6=B8=85=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用gjson.NewWithOptions替代gjson.DecodeToJson解析JSON字符串- 添加日志记录清理过程中的JSON数据 - 修改cleanData函数参数类型为interface{} - 更新错误处理逻辑并添加错误日志 - 调整JSON编码方式,使用ToJson()方法返回结果 - 移除不必要的类型转换和中间变量声明 --- lklsdk/common/json_cleaner.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lklsdk/common/json_cleaner.go b/lklsdk/common/json_cleaner.go index e044f88..16164f2 100644 --- a/lklsdk/common/json_cleaner.go +++ b/lklsdk/common/json_cleaner.go @@ -4,26 +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) { // 解析为JSON - json, err := gjson.DecodeToJson(jsonStr, gjson.Options{StrNumber: true}) - if err != nil { - return nil, err - } - + //var data interface{} + json := gjson.NewWithOptions(jsonStr, gjson.Options{StrNumber: true}) + glog.Infof(gctx.New(), "Cleaning JSON: %+v", json) // 递归清理数据 - cleaned := cleanData(json) - - // 转换回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值