网络编程
位置:首页>> 网络编程>> Go语言>> 解决golang json解析出现值为空的问题

解决golang json解析出现值为空的问题

作者:一只APE的进化史  发布时间:2024-04-27 15:27:58 

标签:golang,json,空值

我是通过beego框架,将请求过来的json进行解析,并将值保存在结构体中


--------------------1---------------------
request := UpdateCommentRequestData{}
req := common.Request{Data: request}
err := json.Unmarshal(controller.Ctx.Input.RequestBody, &req)
------------------------------------------

其中 UpdateCommentRequestData的结构是这样的


type UpdateCommentRequestData struct {
Id []string `json:"id"`
}

common.request的结构是这样的


type Request struct {
UserId uint64 `json:"userId,string"`
Data interface{} `json:"data"`
}

我使用1中的代码进行解析,发现request.Id的值是空的,但是传来的json是存在Id值的,当时一头雾水,就不断在日志中打印,后来定位到是数据类型存在问题,

在1中的代码里面,Data字段传的是request的值,是值的拷贝,也就是说,json解析后的数据并不是赋值到reques中,所以使用request.Id并不会取到值,

如果将代码改成这样,再使用request.Id就可以取到值了

req := common.Request{Data: request},

补充:golang Unmarshal拿不全数据问题

说明:这个问题出现在后端调用json.Unmarshal方法去解析数据库中存的数据时,解析出来的结果中只能拿到部分数据,json格式经检查后正确无误,同时也没有字段名出错等低级错误。

首先来看要解析后的go结构体


type ParamConfig struct {
//标识Id
Id  string
//抓拍目标参数配置
SnapObjConfig *SnapObjConfig
//默认去重参数配置
DefaltDeweightConfig *DefaltDeweightConfig
}
//抓拍目标参数结构
type SnapObjConfig struct {
//分辨率参数
Distinguish *Distinguish
//机动车配置
vehicle *DataConfig
//非机动车配置
nonmotor *DataConfig
//行人配置
pedestrian *DataConfig
//人脸配置
face *DataConfig
}
//分辨率结构
type Distinguish struct {
//分辨率值
DistinguishRate int32
}

type DataConfig struct {
//最小宽度
MinWeight int32
//最小高度
MinHight int32
}
//默认去重参数结构
type DefaltDeweightConfig struct {
vehicle *DeweightNum
nonmotor *DeweightNum
pedestrian *DeweightNum
face *DeweightNum
}
//默认参数值结构
type DeweightNum struct {
Number float32
}

先向数据库中插入一条需要解析的数据

解决golang json解析出现值为空的问题

SQL语句如下所示:


INSERT INTO "public"."sys_config"("config_key", "config_value") VALUES ('param_config', '[{"Id":"8149aa8e-1466-469b-ac5e-b0ea72f96129","SnapObjConfig":{"Distinguish":{"DistinguishRate":270},"vehicle":{"MinWeight":128,"MinHight":128},"nonmotor":{"MinWeight":32,"MinHight":64},"pedestrian":{"MinWeight":32,"MinHight":64},"face":{"MinWeight":40,"MinHight":40}},"DefaltDeweightConfig":{"vehicle":{"Number":0.95},"nonmotor":{"Number":0.95},"pedestrian":{"Number":0.95},"face":{"Number":0.95}}}]');

为了方便说明下面在代码中打上详细的log,大码如下:


func (this *CommonController)GetParamConfig(c *gin.Context) {
searchResp := &models.SearchResp{
Code: models.ApiStatus_SUCCESS,
Msg: "successs",
}
retParamConfig := make([]*ParamConfig, 0)
if configs, err := db_model.SysConfigsByConfigKey(this.DB, ParamConfigKey); err != nil && !models.IsEmptyResults(err){
glog.Infoln(err)
searchResp.Code = models.ApiStatus_ERROR
searchResp.Msg = "fail"
c.JSON(http.StatusInternalServerError, searchResp)
return
} else if len(configs) > 0 {
glog.Infoln("data----------------", configs[0].ConfigValue)
if err := json.Unmarshal([]byte(configs[0].ConfigValue), &retParamConfig); err != nil {
glog.Errorln(err)
searchResp.Code = models.ApiStatus_ERROR
searchResp.Msg = err.Error()
c.JSON(http.StatusInternalServerError, searchResp)
return
}
}
searchResp.Data = retParamConfig
glog.Infoln("retParamConfig[0].SnapObjConfig.Vehicle----------", retParamConfig[0].SnapObjConfig.vehicle)
glog.Infoln("retParamConfig[0].SnapObjConfig.nonmotor-----------", retParamConfig[0].SnapObjConfig.nonmotor)
glog.Infoln("retParamConfig[0].SnapObjConfig.pedestrian------------", retParamConfig[0].SnapObjConfig.pedestrian)
glog.Infoln("retParamConfig[0].SnapObjConfig.Fsce------------------", retParamConfig[0].SnapObjConfig.face)
glog.Infoln("retParamConfig[0].DefaltDeweightConfig.Fsce------------------", retParamConfig[0].DefaltDeweightConfig.face)
glog.Infoln("retParamConfig[0].DefaltDeweightConfig.Fsce------------------", retParamConfig[0].DefaltDeweightConfig.vehicle)
glog.Infoln("retParamConfig[0].DefaltDeweightConfig.Fsce------------------", retParamConfig[0].DefaltDeweightConfig.nonmotor)
glog.Infoln("retParamConfig[0].DefaltDeweightConfig.Fsce------------------", retParamConfig[0].DefaltDeweightConfig.pedestrian)

c.JSON(http.StatusOK, searchResp)
}

运行之后如图所示:

解决golang json解析出现值为空的问题

很明显从一开始我们就向数据库中存入了数据,同时从日志中可以看出,data中存的是去数据库中获取的数据,数据和刚开始存入到数据库中的值一样,但是调用unmarshal之后却获取不到全部的数据,可以看一下使用postman调用接口之后的返回结果如下:

解决golang json解析出现值为空的问题

接口的返回值中只是返回了部分数据,到底是出了什么问题呢?之后我曾仔细的核对完每一个结构字段和数据库中字段的类型,确保并不是这些原因导致的,想了很久不知道这个问题到底是如何发生的,无意中将结构体中的字段名的首字母都变成了大写,经过编译运行之后终于拿到了全部的数据,

解决golang json解析出现值为空的问题

有了这个结果之后,我又去仔细的google了一下这个问题,原来结构体中的每一项如果是导出项的时候首字母必须是大写的,但是问题是SQL语句中在数据库中存入的信息都是首字母小写的,检索出来的结果却是大写的,很明显这个处理过程中大小写匹配的问题被忽略掉了,因此要想按照我们的信息随意匹配的话就得在结构体后面加tag,这样解析时就会只匹配tag中的名字,但是tag中的结果不能为空格否则依旧会报错。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://blog.csdn.net/markcheney/article/details/75675594

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com