亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在這種情況下如何解組 json?

在這種情況下如何解組 json?

Go
蕭十郎 2023-02-28 20:22:42
我必須在 ELK 的 echo 框架中間件中解組 Json(請求,響應主體),就像這段代碼一樣。var reqJSONBody, resJSONBody map[string]interface{}if len(*reqBody) > 0 {    if err := unmarshalJSON(reqBody, &reqJSONBody); err != nil {        gl.ServiceLogger.Error("error parsing the request body: ", requestURI, err)    }    encryptPrivacyField(&reqJSONBody)}if len(*resBody) > 0 && resContentType != "" && strings.Contains(resContentType, "application/json") {    if err := unmarshalJSON(resBody, &resJSONBody); err != nil {        gl.ServiceLogger.Error("error parsing the response body: ", requestURI, err)    }    encryptPrivacyField(&resJSONBody)}這是工作,但是,某些 URI 響應[]map[string]interface{}類型。所以我得到了這個錯誤。json: cannot unmarshal array into Go value of type map[string]interface {}解決問題的最佳方法是什么?
查看完整描述

1 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

我通過更改解決了它:


var reqJSONBody, resJSONBody interface{}

if len(*reqBody) > 0 {

    if err := json.Unmarshal(*reqBody, &reqJSONBody); err != nil {

        gl.ServiceLogger.Error("error parsing the request body: ", requestURI, err)

    }

    encryptPrivacyField(&reqJSONBody)

}

所以我必須encryptPrivacyField像這樣改變方法:


func encryptPrivacyField(data *interface{}) {

switch reflect.TypeOf(*data).Kind() {

case reflect.Map:

    for _, field := range getPrivacyFieldList() {

        if item, ok := (*data).(map[string]interface{})[field]; ok && item != nil {

            (*data).(map[string]interface{})[field] = db.NewEncString(fmt.Sprintf("%v", (*data).(map[string]interface{})[field]))

        }

    }


    for _, field := range getHashFieldList() {

        if item, ok := (*data).(map[string]interface{})[field]; ok && item != nil {

            (*data).(map[string]interface{})[field] = db.NewHashString(fmt.Sprintf("%v", (*data).(map[string]interface{})[field]))

        }

    }

case reflect.Slice:

    for index, _ := range (*data).([]interface{}) {

        encryptPrivacyField(&(*data).([]interface{})[index])

    }


}

}


查看完整回答
反對 回復 2023-02-28
  • 1 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號