将 _id 解析为 id 不起作用。

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

Unmarshal _id to id wont work

问题

我的结构如下:

  1. type Article struct {
  2. ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
  3. LangCode string `json:"langCode" bson:"langCode"`
  4. AuthorId string `json:"authorId" bson:"authorId"`
  5. AuthorName string `json:"authorName" bson:"authorName"`
  6. ArticleType int64 `json:"type" bson:"type"`
  7. Title string `json:"title" bson:"title"`
  8. Intro string `json:"intro" bson:"intro"`
  9. Body string `json:"body" bson:"body"`
  10. MainPic string `json:"mainPic" bson:"mainPic"`
  11. Tags string `json:"tags" bson:"tags"`
  12. Slug string `json:"slug" bson:"slug"`
  13. DateAdded time.Time `json:"dateAdded" bson:"dateAdded"`
  14. Status int64 `json:"status" bson:"status"`
  15. }

以下是代码片段:

  1. pageReturn.Pagination = resultsList.Pagination
  2. err = json.Unmarshal(resultsList.Results, &pageReturn.Articles)

这样返回的数据将不包含数据库中的 _id 值(也就是说,在 JSON 字符串中,id 的值将为空字符串)。

如果我将 ID bson.ObjectId json:"id" bson:"_id,omitempty"改为ID bson.ObjectId json:"_id" bson:"_id,omitempty",则会正常返回值(将返回数据库中实际的 _id 值)。

我想知道如何避免这种情况(但我仍然需要使用 json.Unmarshal)。

英文:

So my structure looks like this:

  1. type Article struct {
  2. ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
  3. LangCode string `json:"langCode" bson:"langCode"`
  4. AuthorId string `json:"authorId" bson:"authorId"`
  5. AuthorName string `json:"authorName" bson:"authorName"`
  6. ArticleType int64 `json:"type" bson:"type"`
  7. Title string `json:"title" bson:"title"`
  8. Intro string `json:"intro" bson:"intro"`
  9. Body string `json:"body" bson:"body"`
  10. MainPic string `json:"mainPic" bson:"mainPic"`
  11. Tags string `json:"tags" bson:"tags"`
  12. Slug string `json:"slug" bson:"slug"`
  13. DateAdded time.Time `json:"dateAdded" bson:"dateAdded"`
  14. Status int64 `json:"status" bson:"status"`
  15. }

And following snippet:

  1. pageReturn.Pagination = resultsList.Pagination
  2. err = json.Unmarshal(resultsList.Results, &pageReturn.Articles)

Will return data without value _id from database (I mean in json string id will be equal to "")

If I change ID bson.ObjectId json:"id" bson:"_id,omitempty"
to ID bson.ObjectId json:"_id" bson:"_id,omitempty"

value will be returned normally (actual _id value from db will be returned)

I'm wondering how can I avoid this (but I still need to use json.Unmarshal)

答案1

得分: 1

  • 使用标签json:"_id"将其解组为您的Article结构体。
  • 只有标签不同的两个结构体类型可以相互转换。因此,一种解决方案是创建另一个ArticleBis类型,其标签为json:"id"。然后,将您的article转换为ArticleBis实例,再进行编组。

另一个简单的示例:

  1. package main
  2. import "fmt"
  3. import "encoding/json"
  4. type Base struct {
  5. Firstname string `json:"first"`
  6. }
  7. type A struct {
  8. Base
  9. Lastname string `json:"last"`
  10. }
  11. type B struct {
  12. Base
  13. Lastname string `json:"lastname"`
  14. }
  15. func main() {
  16. john := A{Base: Base{Firstname: "John"}, Lastname: "Doe"}
  17. john1 := B(john)
  18. john_json, _ := json.Marshal(john)
  19. john1_json, _ := json.Marshal(john1)
  20. fmt.Println(string(john_json))
  21. fmt.Println(string(john1_json))
  22. }

请注意,以上代码是用于演示目的的简化示例,以说明如何在结构体之间进行转换和编组。

英文:
  • Unmarshal into your Article struct, but with tag json:"_id"
  • two struct types that only differ by tags can be converted to each other. So one solution is to create another ArticleBis type, with tag json:"id" instead. Then you convert your article to an ArticleBis instance, which you Marshal.

Another simple example:

<!-- language: lang-go -->

  1. package main
  2. import &quot;fmt&quot;
  3. import &quot;encoding/json&quot;
  4. type Base struct {
  5. Firstname string `json:&quot;first&quot;`
  6. }
  7. type A struct {
  8. Base
  9. Lastname string `json:&quot;last&quot;`
  10. }
  11. type B struct {
  12. Base
  13. Lastname string `json:&quot;lastname&quot;`
  14. }
  15. func main() {
  16. john := A{Base: Base{Firstname: &quot;John&quot;}, Lastname:&quot;Doe&quot;}
  17. john1 := B(john)
  18. john_json, _ := json.Marshal(john)
  19. john1_json, _ := json.Marshal(john1)
  20. fmt.Println(string(john_json))
  21. fmt.Println(string(john1_json))
  22. }

huangapple
  • 本文由 发表于 2017年9月11日 04:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/46145184.html
匿名

发表评论

匿名网友

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

确定