介绍
本篇文章为大家展示了使用golang对json进行解析时出现空值如何解决,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
我是通过beego框架,将请求过来的json进行解析,并将值保存在结构体中
- - - - - - - - - - - - - - - - - - - 1 - - - - - - - - - - - - - - - - - - - - ,request :=, UpdateCommentRequestData {} ,req :=, common.Request{数据:,请求} ,err :=, json.Unmarshal (controller.Ctx.Input.RequestBody,及要求) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
其中UpdateCommentRequestData的结构是这样的
type UpdateCommentRequestData struct { ,Id [] string “json:“id"” }
常见。请求的结构是这样的
type Request struct { ,UserId uint64 “json:“userId, string"” {},Data 接口,“json:“data"” }
我使用1中的代码进行解析,发现request.Id的值是空的,但是传来的json是存在Id值的,当时一头雾水,就不断在日志中打印,后来定位到是数据类型存在问题,
在1中的代码里面,数据字段传的是请求的值,是值的拷贝,也就是说,json解析后的数据并不是赋值到reques中,所以使用request.Id并不会取到值,
如果将代码改成这样,再使用的要求。身份证就可以取到值了
要求:=常见。}{数据:请求,请求
引用><强>补充:golang解组拿不全数据问题强>
说明:这个问题出现在后端调用json。解组方法去解析数据库中存的数据时,解析出来的结果中只能拿到部分数据,json格式经检查后正确无误,同时也没有字段名出错等低级错误。
首先来看要解析后的去结构体
type ParamConfig struct { ,//标识Id ,Id 字符串 ,//抓拍目标参数配置 SnapObjConfig * SnapObjConfig ,//默认去重参数配置 DefaltDeweightConfig * DefaltDeweightConfig }//抓拍目标参数结构 type SnapObjConfig  struct { ,//分辨率参数 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 }先向数据库中插入一条需要解析的数据
SQL语句如下所示:
INSERT INTO “public"干净sys_config" (“config_key",,“config_value"), VALUES (& # 39; param_config& # 39;,, & # 39; [{“Id":“8149 aa8e - 1466 - 469 b - 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}}}] & # 39;);为了方便说明下面在代码中打上详细的日志,大码如下:
func (却;能够* CommonController) GetParamConfig (c * gin.Context), { ,searchResp :=,, models.SearchResp { ,代码:models.ApiStatus_SUCCESS, ,味精:“successs", ,} ,retParamConfig :=, ([] * ParamConfig,, 0) ,if 配置,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) ,返回 ,}else if  len(配置),祝辞,0,{ ,glog.Infoln(“数据- - - - - - - - - - - - - - - - -“,,配置[0].ConfigValue) ,if err :=, json.Unmarshal([]字节(配置[0].ConfigValue),,, retParamConfig);, err !=, nil { ,glog.Errorln (err)=,searchResp.Code  models.ApiStatus_ERROR=,,searchResp.Msg  err.Error () ,c.JSON (http.StatusInternalServerError, searchResp) ,返回 ,} ,}=,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) null null null null null null null null使用golang对json进行解析时出现空值如何解决