无法将字符串解组为Go结构体字段Article.article_type的类型models.ArticleType。

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

cannot unmarshal string into Go struct field Article.article_type of type models.ArticleType

问题

我无法将JSON字段article_type解组为Golang结构Article

我得到了以下错误:

json: 无法将字符串解组为类型为models.ArticleType的Go结构字段Article.article_type

str := []byte(`[{"created_at":1486579331,"updated_at":1486579331,"article_type":"news"}]`)

type Article struct {
    ID            uint      `gorm:"primary_key"`
    CreatedAt     timestamp.Timestamp `json:"created_at"`
    UpdatedAt     timestamp.Timestamp `json:"updated_at"`

    ArticleType   ArticleType `json:"article_type"`
    ArticleTypeId uint        `gorm:"index" json:"-"`

}

type ArticleType struct {
    ID        uint      `gorm:"primary_key" json:"id"`
    CreatedAt timestamp.Timestamp `json:"created_at"`
    UpdatedAt timestamp.Timestamp `json:"updated_at"`
    Title     string    `gorm:"size:255" json:"title"`

    Articles  []Article `json:"articles"`
}

articles := []models.Article{}
if err := json.Unmarshal(str, &articles); err != nil {
    panic(err)
}

我希望"article_type":"news"能够解析为:
Article.ArticleType.title = "news"
然后我可以将具有标题为"news"的文章类型的文章对象保存在数据库中。

英文:

I cannot unmarshal json field article_type into golang struct Article.

I'm getting error:

json: cannot unmarshal string into Go struct field Article.article_type of type models.ArticleType

str := []byte(`[{"created_at":1486579331,"updated_at":1486579331,"article_type":"news"}]`)

type Article struct {
	ID            uint      `gorm:"primary_key"`
	CreatedAt     timestamp.Timestamp `json:"created_at"`
	UpdatedAt     timestamp.Timestamp `json:"updated_at"`

	ArticleType   ArticleType `json:"article_type"`
	ArticleTypeId uint        `gorm:"index" json:"-"`

type ArticleType struct {
	ID        uint      `gorm:"primary_key" json:"id"`
	CreatedAt timestamp.Timestamp `json:"created_at"`
	UpdatedAt timestamp.Timestamp `json:"updated_at"`
	Title	  string    `gorm:"size:255" json:"title"`

	Articles  []Article `json:"articles"`
}

articles := []models.Article{}
if err := json.Unmarshal(str, &articles); err != nil {
    panic(err)
}

I wanted that "article_type":"news" would be parse as:
Article.ArticleType.title = "news"
And then I would can save article object which have article type with title "news" in database.

答案1

得分: 2

你可以让你的ArticleType实现json.Unmarshaler接口,通过在其上定义一个UnmarshalJSON方法:

func (a *ArticleType) UnmarshalJSON(b []byte) error {
    a.Title = string(b)
    return nil
}

https://play.golang.org/p/k_UlghLxZI

英文:

You can have your ArticleType implement the json.Unmarshaler interface by defining an UnmarshalJSON method on it:

func (a *ArticleType) UnmarshalJSON(b []byte) error {
	a.Title = string(b)
	return nil
}

https://play.golang.org/p/k_UlghLxZI

huangapple
  • 本文由 发表于 2017年4月9日 15:54:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/43304365.html
匿名

发表评论

匿名网友

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

确定