解決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 { //標識Id Id string //抓拍目標參數(shù)配置 SnapObjConfig *SnapObjConfig //默認去重參數(shù)配置 DefaltDeweightConfig *DefaltDeweightConfig } //抓拍目標參數(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
- Golang語言JSON解碼函數(shù)Unmarshal的使用
- golang中json的omitempty使用操作
- golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法
- 利用Golang解析json數(shù)據(jù)的方法示例
- Golang實現(xiàn)解析JSON的三種方法總結(jié)
- Golang map如何生成有序的json數(shù)據(jù)詳解
- Golang中使用JSON的一些小技巧分享
- 解決Golang json序列化字符串時多了\的情況
- golang中json反序列化可能遇到的問題
- 深入淺出Go語言:手把手教你高效生成與解析JSON數(shù)據(jù)
相關(guān)文章
Go調(diào)用opencv實現(xiàn)圖片矯正的代碼示例
這篇文章主要為大家詳細介紹了Go調(diào)用opencv實現(xiàn)圖片矯正的代碼示例,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09Golang簡單實現(xiàn)http的server端和client端
Http 服務(wù)是基于 Tcp 的應(yīng)用層的實現(xiàn),也是最常見的網(wǎng)絡(luò)協(xié)議之一。本文主要介紹了Golang簡單實現(xiàn)http的server端和client端,感興趣的可以了解一下2021-06-06