json.NewDecoder(r.Body).Decode(&admin) 返回空的结构体。

huangapple go评论87阅读模式
英文:

json.NewDecoder(r.Body).Decode(&admin) returns empty struct

问题

我知道有很多人遇到了同样的问题,但我仍然在这里。我非常确定我的代码是正确的,但结果的结构体为空。

函数:

func PostAdminHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-type", "application/json")
	var admin admin.Admin

	json.NewDecoder(r.Body).Decode(&admin)
	fmt.Println(admin)
	_, err := PostAdmin(admin)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

控制台输出:

{     ObjectID("000000000000000000000000")}

结构体:

package entity

import "go.mongodb.org/mongo-driver/bson/primitive"

type Admin struct {
	FirstName string
	LastName  string
	Email     string
	Password  string
	Role      string
	Campus    primitive.ObjectID
}

路由:

adminRoute.HandleFunc("/admin", admin.PostAdminHandler).Methods("POST")

我通过Insomnia发送的JSON数据:

{
	"FirstName": "Jeanne",
	"LastName": "Darc",
	"Email": "jeanne.darc@rouen.fr",
	"Password": "JeanneDarc2022",
	"Role": "admin",
	"Campus": "60d5a25ff4d722d3b77d1929"
}

解码器报错信息:

invalid character '}' looking for beginning of object key string
英文:

I know there are a lots of people that has run into the same issue but still here I am. I'm pretty sure my code is correct and still the resulting struct is empty.
Function :

func PostAdminHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-type", "application/json")
	var admin admin.Admin

	json.NewDecoder(r.Body).Decode(&admin)
	fmt.Println(admin)
	_, err := PostAdmin(admin)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

Console print :

{     ObjectID("000000000000000000000000")}

Structure :

package entity

import "go.mongodb.org/mongo-driver/bson/primitive"

type Admin struct {
	FirstName string
	LastName  string
	Email     string
	Password  string
	Role      string
	Campus    primitive.ObjectID
}

Route :

	adminRoute.HandleFunc("/admin", admin.PostAdminHandler).Methods("POST")

Json data I'm sending through Insomnia :

{
	"FirstName": "Jeanne",
	"LastName": "Darc",
	"Email": "jeanne.darc@rouen.fr",
	"Password": "JeanneDarc2022",
	"Role": "admin",
	"Campus": "60d5a25ff4d722d3b77d1929",
}

Error i'm getting from decoder :

invalid character '}' looking for beginning of object key string

答案1

得分: 4

这个RFC:

https://datatracker.ietf.org/doc/html/rfc7159

将JSON对象的格式定义为:

> 对象结构由一对花括号括起来,其中包含零个或多个名称/值对(或成员)。名称是一个字符串。每个名称后面跟着一个冒号,将名称与值分隔开来。一个逗号将一个值与后面的名称分隔开来。对象中的名称应该是唯一的。

object = begin-object [ member *( value-separator member ) ]
               end-object

member = string name-separator value

所以,不允许有尾随逗号。

请删除输入中的最后一个逗号。

英文:

This RFC:

https://datatracker.ietf.org/doc/html/rfc7159

specifies the JSON object format as:

> An object structure is represented as a pair of curly brackets
> surrounding zero or more name/value pairs (or members). A name is a
> string. A single colon comes after each name, separating the name
> from the value. A single comma separates a value from a following
> name. The names within an object SHOULD be unique.

object = begin-object [ member *( value-separator member ) ]
               end-object

member = string name-separator value

So, no trailing commas.

Remove the last comma in the input.

huangapple
  • 本文由 发表于 2021年6月25日 20:56:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/68131353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定