解決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}
先向數據庫中插入一條需要解析的數據
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)}
運行之后如圖所示:
很明顯從一開始我們就向數據庫中存入了數據,同時從日志中可以看出,data中存的是去數據庫中獲取的數據,數據和剛開始存入到數據庫中的值一樣,但是調用unmarshal之后卻獲取不到全部的數據,可以看一下使用postman調用接口之后的返回結果如下:
接口的返回值中只是返回了部分數據,到底是出了什么問題呢?之后我曾仔細的核對完每一個結構字段和數據庫中字段的類型,確保并不是這些原因導致的,想了很久不知道這個問題到底是如何發生的,無意中將結構體中的字段名的首字母都變成了大寫,經過編譯運行之后終于拿到了全部的數據,
有了這個結果之后,我又去仔細的google了一下這個問題,原來結構體中的每一項如果是導出項的時候首字母必須是大寫的,但是問題是SQL語句中在數據庫中存入的信息都是首字母小寫的,檢索出來的結果卻是大寫的,很明顯這個處理過程中大小寫匹配的問題被忽略掉了,因此要想按照我們的信息隨意匹配的話就得在結構體后面加tag,這樣解析時就會只匹配tag中的名字,但是tag中的結果不能為空格否則依舊會報錯。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章:
