解決golang json解析出現(xiàn)值為空的問題
我是通過beego框架,將請求過來的json進行解析,并將值保存在結(jié)構(gòu)體中
--------------------1--------------------- request := UpdateCommentRequestData{} req := common.Request{Data: request} err := json.Unmarshal(controller.Ctx.Input.RequestBody, &req)------------------------------------------
其中 UpdateCommentRequestData的結(jié)構(gòu)是這樣的
type UpdateCommentRequestData struct { Id []string `json:'id'`}
common.request的結(jié)構(gòu)是這樣的
type Request struct { UserId uint64 `json:'userId,string'` Data interface{} `json:'data'`}
我使用1中的代碼進行解析,發(fā)現(xiàn)request.Id的值是空的,但是傳來的json是存在Id值的,當(dāng)時一頭霧水,就不斷在日志中打印,后來定位到是數(shù)據(jù)類型存在問題,
在1中的代碼里面,Data字段傳的是request的值,是值的拷貝,也就是說,json解析后的數(shù)據(jù)并不是賦值到reques中,所以使用request.Id并不會取到值,
如果將代碼改成這樣,再使用request.Id就可以取到值了
req := common.Request{Data: request},
補充:golang Unmarshal拿不全數(shù)據(jù)問題
說明:這個問題出現(xiàn)在后端調(diào)用json.Unmarshal方法去解析數(shù)據(jù)庫中存的數(shù)據(jù)時,解析出來的結(jié)果中只能拿到部分數(shù)據(jù),json格式經(jīng)檢查后正確無誤,同時也沒有字段名出錯等低級錯誤。
首先來看要解析后的go結(jié)構(gòu)體
type ParamConfig struct { //標(biāo)識Id Id string //抓拍目標(biāo)參數(shù)配置 SnapObjConfig *SnapObjConfig //默認去重參數(shù)配置 DefaltDeweightConfig *DefaltDeweightConfig}//抓拍目標(biāo)參數(shù)結(jié)構(gòu)type SnapObjConfig struct { //分辨率參數(shù) Distinguish *Distinguish //機動車配置 vehicle *DataConfig //非機動車配置 nonmotor *DataConfig //行人配置 pedestrian *DataConfig //人臉配置 face *DataConfig}//分辨率結(jié)構(gòu)type Distinguish struct { //分辨率值 DistinguishRate int32} type DataConfig struct { //最小寬度 MinWeight int32 //最小高度 MinHight int32}//默認去重參數(shù)結(jié)構(gòu)type DefaltDeweightConfig struct { vehicle *DeweightNum nonmotor *DeweightNum pedestrian *DeweightNum face *DeweightNum}//默認參數(shù)值結(jié)構(gòu)type DeweightNum struct { Number float32}
先向數(shù)據(jù)庫中插入一條需要解析的數(shù)據(jù)
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)}
運行之后如圖所示:
很明顯從一開始我們就向數(shù)據(jù)庫中存入了數(shù)據(jù),同時從日志中可以看出,data中存的是去數(shù)據(jù)庫中獲取的數(shù)據(jù),數(shù)據(jù)和剛開始存入到數(shù)據(jù)庫中的值一樣,但是調(diào)用unmarshal之后卻獲取不到全部的數(shù)據(jù),可以看一下使用postman調(diào)用接口之后的返回結(jié)果如下:
接口的返回值中只是返回了部分數(shù)據(jù),到底是出了什么問題呢?之后我曾仔細的核對完每一個結(jié)構(gòu)字段和數(shù)據(jù)庫中字段的類型,確保并不是這些原因?qū)е碌模肓撕芫貌恢肋@個問題到底是如何發(fā)生的,無意中將結(jié)構(gòu)體中的字段名的首字母都變成了大寫,經(jīng)過編譯運行之后終于拿到了全部的數(shù)據(jù),
有了這個結(jié)果之后,我又去仔細的google了一下這個問題,原來結(jié)構(gòu)體中的每一項如果是導(dǎo)出項的時候首字母必須是大寫的,但是問題是SQL語句中在數(shù)據(jù)庫中存入的信息都是首字母小寫的,檢索出來的結(jié)果卻是大寫的,很明顯這個處理過程中大小寫匹配的問題被忽略掉了,因此要想按照我們的信息隨意匹配的話就得在結(jié)構(gòu)體后面加tag,這樣解析時就會只匹配tag中的名字,但是tag中的結(jié)果不能為空格否則依舊會報錯。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
