解決golang json解析出現(xiàn)值為空的問(wèn)題
我是通過(guò)beego框架,將請(qǐng)求過(guò)來(lái)的json進(jìn)行解析,并將值保存在結(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中的代碼進(jìn)行解析,發(fā)現(xiàn)request.Id的值是空的,但是傳來(lái)的json是存在Id值的,當(dāng)時(shí)一頭霧水,就不斷在日志中打印,后來(lái)定位到是數(shù)據(jù)類(lèi)型存在問(wèn)題,
在1中的代碼里面,Data字段傳的是request的值,是值的拷貝,也就是說(shuō),json解析后的數(shù)據(jù)并不是賦值到reques中,所以使用request.Id并不會(huì)取到值,
如果將代碼改成這樣,再使用request.Id就可以取到值了
req := common.Request{Data: request},
補(bǔ)充:golang Unmarshal拿不全數(shù)據(jù)問(wèn)題
說(shuō)明:這個(gè)問(wèn)題出現(xiàn)在后端調(diào)用json.Unmarshal方法去解析數(shù)據(jù)庫(kù)中存的數(shù)據(jù)時(shí),解析出來(lái)的結(jié)果中只能拿到部分?jǐn)?shù)據(jù),json格式經(jīng)檢查后正確無(wú)誤,同時(shí)也沒(méi)有字段名出錯(cuò)等低級(jí)錯(cuò)誤。
首先來(lái)看要解析后的go結(jié)構(gòu)體
type ParamConfig struct { //標(biāo)識(shí)Id Id string //抓拍目標(biāo)參數(shù)配置 SnapObjConfig *SnapObjConfig //默認(rèn)去重參數(shù)配置 DefaltDeweightConfig *DefaltDeweightConfig}//抓拍目標(biāo)參數(shù)結(jié)構(gòu)type SnapObjConfig struct { //分辨率參數(shù) Distinguish *Distinguish //機(jī)動(dòng)車(chē)配置 vehicle *DataConfig //非機(jī)動(dòng)車(chē)配置 nonmotor *DataConfig //行人配置 pedestrian *DataConfig //人臉配置 face *DataConfig}//分辨率結(jié)構(gòu)type Distinguish struct { //分辨率值 DistinguishRate int32} type DataConfig struct { //最小寬度 MinWeight int32 //最小高度 MinHight int32}//默認(rèn)去重參數(shù)結(jié)構(gòu)type DefaltDeweightConfig struct { vehicle *DeweightNum nonmotor *DeweightNum pedestrian *DeweightNum face *DeweightNum}//默認(rèn)參數(shù)值結(jié)構(gòu)type DeweightNum struct { Number float32}
先向數(shù)據(jù)庫(kù)中插入一條需要解析的數(shù)據(jù)
SQL語(yǔ)句如下所示:
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}}}]’);
為了方便說(shuō)明下面在代碼中打上詳細(xì)的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)}
運(yùn)行之后如圖所示:
很明顯從一開(kāi)始我們就向數(shù)據(jù)庫(kù)中存入了數(shù)據(jù),同時(shí)從日志中可以看出,data中存的是去數(shù)據(jù)庫(kù)中獲取的數(shù)據(jù),數(shù)據(jù)和剛開(kāi)始存入到數(shù)據(jù)庫(kù)中的值一樣,但是調(diào)用unmarshal之后卻獲取不到全部的數(shù)據(jù),可以看一下使用postman調(diào)用接口之后的返回結(jié)果如下:
接口的返回值中只是返回了部分?jǐn)?shù)據(jù),到底是出了什么問(wèn)題呢?之后我曾仔細(xì)的核對(duì)完每一個(gè)結(jié)構(gòu)字段和數(shù)據(jù)庫(kù)中字段的類(lèi)型,確保并不是這些原因?qū)е碌?,想了很久不知道這個(gè)問(wèn)題到底是如何發(fā)生的,無(wú)意中將結(jié)構(gòu)體中的字段名的首字母都變成了大寫(xiě),經(jīng)過(guò)編譯運(yùn)行之后終于拿到了全部的數(shù)據(jù),
有了這個(gè)結(jié)果之后,我又去仔細(xì)的google了一下這個(gè)問(wèn)題,原來(lái)結(jié)構(gòu)體中的每一項(xiàng)如果是導(dǎo)出項(xiàng)的時(shí)候首字母必須是大寫(xiě)的,但是問(wèn)題是SQL語(yǔ)句中在數(shù)據(jù)庫(kù)中存入的信息都是首字母小寫(xiě)的,檢索出來(lái)的結(jié)果卻是大寫(xiě)的,很明顯這個(gè)處理過(guò)程中大小寫(xiě)匹配的問(wèn)題被忽略掉了,因此要想按照我們的信息隨意匹配的話就得在結(jié)構(gòu)體后面加tag,這樣解析時(shí)就會(huì)只匹配tag中的名字,但是tag中的結(jié)果不能為空格否則依舊會(huì)報(bào)錯(cuò)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. JavaWeb Servlet中url-pattern的使用2. jsp中sitemesh修改tagRule技術(shù)分享3. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼4. React優(yōu)雅的封裝SvgIcon組件示例5. 輕松學(xué)習(xí)XML教程6. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除9. ASP基礎(chǔ)知識(shí)VBScript基本元素講解10. 詳解瀏覽器的緩存機(jī)制
