国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

解決golang json解析出現(xiàn)值為空的問題

瀏覽:101日期:2024-05-30 10:22:35

我是通過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ù)

解決golang json解析出現(xiàn)值為空的問題

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解析出現(xiàn)值為空的問題

很明顯從一開始我們就向數(shù)據(jù)庫中存入了數(shù)據(jù),同時從日志中可以看出,data中存的是去數(shù)據(jù)庫中獲取的數(shù)據(jù),數(shù)據(jù)和剛開始存入到數(shù)據(jù)庫中的值一樣,但是調(diào)用unmarshal之后卻獲取不到全部的數(shù)據(jù),可以看一下使用postman調(diào)用接口之后的返回結(jié)果如下:

解決golang json解析出現(xiàn)值為空的問題

接口的返回值中只是返回了部分數(shù)據(jù),到底是出了什么問題呢?之后我曾仔細的核對完每一個結(jié)構(gòu)字段和數(shù)據(jù)庫中字段的類型,確保并不是這些原因?qū)е碌模肓撕芫貌恢肋@個問題到底是如何發(fā)生的,無意中將結(jié)構(gòu)體中的字段名的首字母都變成了大寫,經(jīng)過編譯運行之后終于拿到了全部的數(shù)據(jù),

解決golang json解析出現(xiàn)值為空的問題

有了這個結(jié)果之后,我又去仔細的google了一下這個問題,原來結(jié)構(gòu)體中的每一項如果是導(dǎo)出項的時候首字母必須是大寫的,但是問題是SQL語句中在數(shù)據(jù)庫中存入的信息都是首字母小寫的,檢索出來的結(jié)果卻是大寫的,很明顯這個處理過程中大小寫匹配的問題被忽略掉了,因此要想按照我們的信息隨意匹配的話就得在結(jié)構(gòu)體后面加tag,這樣解析時就會只匹配tag中的名字,但是tag中的結(jié)果不能為空格否則依舊會報錯。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 富顺县| 黔江区| 天门市| 武清区| 南华县| 永州市| 通山县| 成武县| 固始县| 池州市| 沾益县| 大邑县| 华安县| 佛坪县| 万全县| 营山县| 富平县| 明星| 安义县| 改则县| 肇庆市| 六枝特区| 武强县| 平顺县| 鹤壁市| 瓦房店市| 太保市| 图片| 济南市| 郓城县| 民乐县| 平南县| 莆田市| 金塔县| 桃江县| 同心县| 奇台县| 闽侯县| 吐鲁番市| 曲阜市| 剑阁县|