取消编组时 Golang 中的时间解析错误

Time parsing error in Golang while Un-Marshalling

提问人:Aditya Sinha 提问时间:4/12/2023 最后编辑:protobAditya Sinha 更新时间:4/12/2023 访问量:124

问:

JSON 请求正文:

{
        "ifr": "b997c96cb360",
        "ste": "EN",
        "tye": "random",
        "description": "desc",
        "creationDate": "2023-04-06 06:01:24",
        "modifiedDate": "2023-04-11 09:44:48"
}

结构:

type M struct {
 Ifr           string             `xml:"ifr,omitempty" json:"ifr,omitempty" validate:"required"`
 Ste           string             `xml:"ste,omitempty" json:"ste,omitempty"`
 Tye           string             `json:"tye,omitempty" xml:"tye,omitempty"`
 Description   string             `xml:"description,omitempty" json:"description,omitempty"`
 CreationDate  time.Time          `xml:"creationDate,omitempty" json:"creationDate,omitempty"`
 ModifiedDate  time.Time          `xml:"modifiedDate,omitempty" json:"modifiedDate,omitempty"`
}

**当上面的json正文被传递到取消编组时,我收到这个错误:

error(time.ParseError) {布局: “”2006-01-02T15:04:05Z07:00“”, 值: “”2023-04-06 06:01:24“”, LayoutElem: “T”, ValueElem: ” 06:01:24“”,消息:“”}

功能:

func ConvertRequestBodyIntoCustomStruct(ctx *gin.Context, structure interface{}) *appError.AppError {
    //Reading the request body
    body, _ := ioutil.ReadAll(ctx.Request.Body)

    //obtaining the content-type
    contentType := ctx.Request.Header.Get("Content-Type")

    
    //un-marshalling the request based on content-type
    if contentType == constants.APPLICAITON_XML {
        err := xml.Unmarshal(body, &structure)
        if err != nil {
            return appError.NewValidationError(appError.ErrInUnMarshalling, appError.ErrInUnMarshalling, appError.ErrInUnMarshalling)
        }
    } else {
        err := json.Unmarshal(body, &structure)
        if err != nil {
            return appError.NewValidationError(appError.ErrInUnMashling, appError.ErrCodeInUnMashling, appError.ErrInUnMashling)
        }
    }

    return nil
}

我希望它在解组时不会抛出上述错误。

go unmarshalling go-gin 日期时间解析

评论

1赞 Burak Serdar 4/12/2023
输入的日期时间格式不是标准格式。您必须取消封送这些日期时间字段为字符串的结构,然后自行分析日期时间值。或者,使用自己的 unmarshal 方法定义自定义类型,并手动分析日期时间。

答:

0赞 Coder 4/12/2023 #1

您遇到的错误与 JSON 请求正文中 CreationDate 和 ModifiedDate 字段的格式有关。根据错误信息,用于解析时间字符串的布局为“2006-01-02T15:04:05Z07:00”,但请求正文中的实际值为“2023-04-06 06:01:24”。

若要解决此问题,需要更新用于分析时间字符串的布局,以匹配 JSON 请求正文中时间值的格式。根据您提供的示例 JSON 请求正文,正确的布局应为“2006-01-02 15:04:05”。

下面是具有正确布局的代码的更新版本:Here's an updated version of your code with the correct layout:

func ConvertRequestBodyIntoCustomStruct(ctx *gin.Context, structure interface{}) *appError.AppError {
    // Reading the request body
    body, _ := ioutil.ReadAll(ctx.Request.Body)

    // Obtaining the content-type
    contentType := ctx.Request.Header.Get("Content-Type")

    // Unmarshalling the request based on content-type
    if contentType == constants.APPLICAITON_XML {
        err := xml.Unmarshal(body, &structure)
        if err != nil {
            return appError.NewValidationError(appError.ErrInUnMarshalling, appError.ErrInUnMarshalling, appError.ErrInUnMarshalling)
        }
    } else {
        // Define a custom time layout
        const customTimeLayout = "2006-01-02 15:04:05"
        
        // Unmarshal using the custom time layout
        err := json.Unmarshal(body, &structure)
        if err != nil {
            return appError.NewValidationError(appError.ErrInUnMashling, appError.ErrCodeInUnMashling, appError.ErrInUnMashling)
        }
        // Update CreationDate and ModifiedDate fields with custom time layout
        if m, ok := structure.(*M); ok {
            m.CreationDate, err = time.Parse(customTimeLayout, m.CreationDate.Format(customTimeLayout))
            if err != nil {
                return appError.NewValidationError(appError.ErrInUnMashling, appError.ErrCodeInUnMashling, appError.ErrInUnMashling)
            }
            m.ModifiedDate, err = time.Parse(customTimeLayout, m.ModifiedDate.Format(customTimeLayout))
            if err != nil {
                return appError.NewValidationError(appError.ErrInUnMashling, appError.ErrCodeInUnMashling, appError.ErrInUnMashling)
            }
        }
    }

    return nil
}

通过更新时间布局以匹配 JSON 请求正文中时间值的格式,您应该能够成功取消封送请求,而不会遇到“时间。ParseError“错误。

评论

0赞 Aditya Sinha 4/12/2023
嗨,Vikas,实际上我们在 else 块中解组自身时出现错误,并且它不会前进到日期格式代码