json: 无法将字符串解组到 Go 结构字段中
json: cannot unmarshal string into Go struct field
我想创建一个 CRUD rest API 来使用 Gorm 和 Gin 管理转换。当我在我的两个模型之间添加关系时,我无法创建转换,因为将字符串 ID 转换为结构类型。
我的模特:
type Cast struct {
ID string `sql:"type:uuid;primary_key;default:uuid_generate_v4()"`
FullName string `gorm:"size:150;not null" json:"full_name"`
NickNames string `gorm:"size:250;null;" json:"nick_names"`
BornLocation Country `gorm:"many2many:CastBornLocation;association_foreignkey:ID;foreignkey:ID" json:"born_location"`
Nationalities []Country `gorm:"many2many:Castnationalities;association_foreignkey:ID;foreignkey:ID" json:"cast_nationalities"`
MiniBio string `gorm:"size:1000;null;" json:"mini_bio"`
}
type Country struct {
ID string `sql:"type:uuid;primary_key;default:uuid_generate_v4()"`
Title string `gorm:"size:100;not null" json:"title"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}
这是控制器:
func (server *Server) CreateCast(c *gin.Context) {
errList = map[string]string{}
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
errList["Invalid_body"] = "Unable to get request"
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusUnprocessableEntity,
"error": errList,
})
return
}
item := models.Cast{}
err = json.Unmarshal(body, &item)
if err != nil {
fmt.Println("---------------------------------")
fmt.Println(err)
fmt.Println("---------------------------------")
errList["Unmarshal_error"] = "Cannot unmarshal body"
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusUnprocessableEntity,
"error": errList,
})
return
}
...
这是我要提交给 API 的 JSON 正文:
{
"full_name": "Cast fullname",
"nick_names": "nickname1, nickname2",
"born_location": "01346a2e-ae50-45aa-8b3e-a66748a76955",
"Nationalities": [
"c370aa49-b39d-4797-a096-094b84903f27",
"01346a2e-ae50-45aa-8b3e-a66748a76955"
],
"mini_bio": "this is the mini bio of the cast"
}
这是完整的打印错误:
json: cannot unmarshal string into Go struct field Cast.born_location of type models.Country
您不能将字符串解组为 Struct 类型。 BornLocation
是国家/地区结构类型,但您在 JSON 中发送的是字符串类型的唯一 ID。 Nationalities
也一样。尝试在对象内部的 id 节点中发送 id 以映射到您的结构。
{
"full_name": "Cast fullname",
"nick_names": "nickname1, nickname2",
"born_location": {
"id" :"01346a2e-ae50-45aa-8b3e-a66748a76955"
}
"Nationalities": [
{
"id" :"c370aa49-b39d-4797-a096-094b84903f27"
},
{
"id" :"01346a2e-ae50-45aa-8b3e-a66748a76955"
}
],
"mini_bio": "this is the mini bio of the cast"
}
或者为您的请求正文创建另一个结构来映射您当前的 JSON。
我想创建一个 CRUD rest API 来使用 Gorm 和 Gin 管理转换。当我在我的两个模型之间添加关系时,我无法创建转换,因为将字符串 ID 转换为结构类型。
我的模特:
type Cast struct {
ID string `sql:"type:uuid;primary_key;default:uuid_generate_v4()"`
FullName string `gorm:"size:150;not null" json:"full_name"`
NickNames string `gorm:"size:250;null;" json:"nick_names"`
BornLocation Country `gorm:"many2many:CastBornLocation;association_foreignkey:ID;foreignkey:ID" json:"born_location"`
Nationalities []Country `gorm:"many2many:Castnationalities;association_foreignkey:ID;foreignkey:ID" json:"cast_nationalities"`
MiniBio string `gorm:"size:1000;null;" json:"mini_bio"`
}
type Country struct {
ID string `sql:"type:uuid;primary_key;default:uuid_generate_v4()"`
Title string `gorm:"size:100;not null" json:"title"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}
这是控制器:
func (server *Server) CreateCast(c *gin.Context) {
errList = map[string]string{}
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
errList["Invalid_body"] = "Unable to get request"
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusUnprocessableEntity,
"error": errList,
})
return
}
item := models.Cast{}
err = json.Unmarshal(body, &item)
if err != nil {
fmt.Println("---------------------------------")
fmt.Println(err)
fmt.Println("---------------------------------")
errList["Unmarshal_error"] = "Cannot unmarshal body"
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusUnprocessableEntity,
"error": errList,
})
return
}
...
这是我要提交给 API 的 JSON 正文:
{
"full_name": "Cast fullname",
"nick_names": "nickname1, nickname2",
"born_location": "01346a2e-ae50-45aa-8b3e-a66748a76955",
"Nationalities": [
"c370aa49-b39d-4797-a096-094b84903f27",
"01346a2e-ae50-45aa-8b3e-a66748a76955"
],
"mini_bio": "this is the mini bio of the cast"
}
这是完整的打印错误:
json: cannot unmarshal string into Go struct field Cast.born_location of type models.Country
您不能将字符串解组为 Struct 类型。 BornLocation
是国家/地区结构类型,但您在 JSON 中发送的是字符串类型的唯一 ID。 Nationalities
也一样。尝试在对象内部的 id 节点中发送 id 以映射到您的结构。
{
"full_name": "Cast fullname",
"nick_names": "nickname1, nickname2",
"born_location": {
"id" :"01346a2e-ae50-45aa-8b3e-a66748a76955"
}
"Nationalities": [
{
"id" :"c370aa49-b39d-4797-a096-094b84903f27"
},
{
"id" :"01346a2e-ae50-45aa-8b3e-a66748a76955"
}
],
"mini_bio": "this is the mini bio of the cast"
}
或者为您的请求正文创建另一个结构来映射您当前的 JSON。