从 Go 将 json 转换为结构时出错

 func MakeMap(w http.ResponseWriter, r *http.Request) {

    // userInfo := context.Get(r, "userInfo").(model.User)

    type _getData struct {

        Title string   `json:"title"`

        Tag   []string `json:"tag"`

    }

    var getData _getData

    err := json.NewDecoder(r.Body).Decode(&getData)

    if err != nil {

        panic(err.Error())

    }

    fmt.Print(getData)


}

当我运行上面的代码时,我得到以下错误


2021/08/24 13:56:54 http: panic serving 127.0.0.1:50619: runtime error: invalid memory address or nil pointer dereference

goroutine 23 [running]:

net/http.(*conn).serve.func1(0x140001e9180)

        /usr/local/go/src/net/http/server.go:1824 +0x108

panic(0x10505b860, 0x10522f240)

        /usr/local/go/src/runtime/panic.go:971 +0x3f4

traveling/controller/mapController.MakeMap(0x1050b5630, 0x140001f40e0, 0x1400018aa00)

/Users/choeyunseog/traveling/traveling/controller/mapController/mapController.go:20 +0x3c

我刚刚开始学习,我不确定为什么我有这个问题,请帮忙


在此处输入图像描述


err := json.NewDecoder(r.Body).Decode(&getData) 

当我像上面一样更改代码行20时,我收到以下错误


 2021/08/24 14:16:44 http: panic serving 127.0.0.1:51396: invalid character '-' in numeric literal

goroutine 23 [running]:

net/http.(*conn).serve.func1(0x140001e9360)

        /usr/local/go/src/net/http/server.go:1824 +0x108

panic(0x100d85d00, 0x14000206070)

        /usr/local/go/src/runtime/panic.go:971 +0x3f4

traveling/controller/mapController.MakeMap(0x100df1630, 0x140001f40e0, 0x1400018aa00)

        /Users/choeyunseog/traveling/traveling/controller/mapController/mapController.go:24 +0x194

net/http.HandlerFunc.ServeHTTP(0x100de75d8, 0x100df1630, 0x140001f40e0, 0x1400018aa00)

        /usr/local/go/src/net/http/server.go:2069 +0x40


子衿沉夜
浏览 136回答 2
2回答

HUH函数

要从 POST/PUT/PATCH 请求的正文获取多部分表单数据,可以使用解析多部分表单方法来解析正文,然后通过&nbsp;PostForm&nbsp;字段访问数据。或者,您可以使用&nbsp;FormValue&nbsp;仅获取与表单字段关联的第一个值。maxMemory := 32<<20if err := r.ParseMultipartForm(maxMemory); err != nil {&nbsp; &nbsp; panic(err)}fmt.Println(_getData{&nbsp; &nbsp; Title: r.FormValue("title"), // FormValue returns string&nbsp; &nbsp; Tag:&nbsp; &nbsp;r.PostForm["tag[]"],&nbsp; // PostForm is a map of []string})

跃然一笑

您可以使用包 github.com/senpathi/paramex 将表单数据解析为 json,如带注释的结构。结构字段必须使用关键字进行注释,并且标记名称是表单数据的键。param您的结构应如下所示。type _getData struct {&nbsp; &nbsp; &nbsp; &nbsp; Title string&nbsp; &nbsp;`param:"title"`&nbsp; &nbsp; &nbsp; &nbsp; Tag&nbsp; &nbsp;[]string `param:"tag[]"`&nbsp; &nbsp; }这是问题中提到的邮递员请求的更新的 MakeMap 处理程序函数func MakeMap(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; // userInfo := context.Get(r, "userInfo").(model.User)&nbsp; &nbsp; type _getData struct {&nbsp; &nbsp; &nbsp; &nbsp; Title string&nbsp; &nbsp;`param:"title"`&nbsp; &nbsp; &nbsp; &nbsp; Tag&nbsp; &nbsp;[]string `param:"tag[]"`&nbsp; &nbsp; }&nbsp; &nbsp; // this needed because u send data from Postman as multipart/form-data&nbsp; &nbsp; maxMemory := 32<<20&nbsp; &nbsp; if err := r.ParseMultipartForm(int64(maxMemory)); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; var getData _getData&nbsp; &nbsp; extractor := paramex.NewParamExtractor()&nbsp; &nbsp; err := extractor.ExtractForms(&getData, r)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Print(getData)&nbsp; &nbsp; //Output: {defaultMap [travelling travelling2]}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go