Go 试图格式化API的响应,但我没有';我不知道怎么做

Go 试图格式化API的响应,但我没有';我不知道怎么做,go,go-gorm,gorm-mysql,Go,Go Gorm,Gorm Mysql,您好,开发人员我最近遇到了一个问题,我试图创建一个稍微提前的查询,其中包含多个2多个关系,并且属于关系,我使用GORM成功地获取了所有这些关系,但是我想将响应格式化为我的WIN结构,我不能在这里执行我的查询i GORM var p []modules.Posts tm := modules.Tags{} db.First(&tm , tagID) //tag and posts has many 2 many rel //post belongs to user tagMaps :=

您好,开发人员我最近遇到了一个问题,我试图创建一个稍微提前的查询,其中包含多个2多个关系,并且属于关系,我使用GORM成功地获取了所有这些关系,但是我想将响应格式化为我的WIN结构,我不能在这里执行我的查询i GORM

var p []modules.Posts
tm := modules.Tags{}
db.First(&tm , tagID)
//tag and posts has many 2 many rel
//post belongs to user 
tagMaps := db.Model(&tm).Preload("TagMaps").Preload("User").Related(&p,"Posts")
我正试图对这样的事情做出回应

[
    {
        "id": "post_id",
        "title": "post_title",
        "image" :" post_image",

        "tags":[
            {"name" : "tag_name" , "id" : "tag_id"},
            {"name" : "tag_name" , "id" : "tag_id"},
            {"name" : "tag_name" , "id" : "tag_id"}

        ],
        "user":{
            "id": "user_id",
            "avatar": " user_avatar"
        }

    }
]
这就是gorm的反应,知道吗

  {
    "id": 2,
    "title": "thos lore for other users lorem 2",
    "image": "",
    "User": {
        "ID": 1,
        "avatar": "url",
        "user_name": "user 5",
        "email": "newadmin@test.com",
        "password": "password",
        "status": "pea",
        "google_id": "google",
        "facebook_id": "face",
        "account_type": "fb",
        "CreatedAt": "2020-04-02T20:35:38+02:00",
        "UpdatedAt": "2020-04-02T20:35:38+02:00",
        "DeletedAt": null,
        "Posts": null
    },
    "UserRefer": 1,
    "created_at": "2020-04-02T20:36:05+02:00",
    "updated_at": "2020-04-02T20:36:05+02:00",
    "deleted_at": null,
    "TagMaps": [{
            "id": 1,
            "tag_name": "asda",
            "CreatedAt": "2020-04-15T23:25:05+02:00",
            "UpdatedAt": "2020-04-17T21:39:26+02:00",
            "DeletedAt": null,
            "Posts": null
        },
        {
            "id": 2,
            "tag_name": "name",
            "CreatedAt": "2020-04-15T23:25:05+02:00",
            "UpdatedAt": "2020-04-17T21:39:26+02:00",
            "DeletedAt": null,
            "Posts": null
        }
    ]
  },
这是我当前的结构

type Posts struct {
    ID           uint        `json: "id"`
    Title        string      `json: "title"`
    Image        string      `json: "image"`
    User         *User `gorm: "foreignkey:UserRefer"` // use UserRefer as foreign key
    UserRefer uint
    CreatedAt    time.Time   `json: "created_at"`
    UpdatedAt    time.Time   `json: "updated_at"`
    DeletedAt    *time.Time  `sql: "index" json: "deleted_at"`
    TagMaps      []Tags `gorm: "many2many:tag_maps;association_autoupdate:false;association_autocreate:false;"`
}

type Tags struct {
    ID        uint   `gorm: "primary_key" json: "id"`
    Name      string `json: "tag_name"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time `sql: "index"`

    Posts        []*Posts `gorm: "many2many:tag_maps"`
}

type User struct {
    ID          uint   `gorm: "primary_key"`
    Avatar      string `json: "avatar" `
    UserName    string `json: "user_name"`
    Password    string `json: "password"`
    Bio         string `json: "bio"`
    GoogleID    string `json: "google_id"`
    FacebookID  string `json: "facebook_id"`
    AccountType string `json: "account_type"`
    CreatedAt   time.Time
    UpdatedAt   time.Time
    DeletedAt   *time.Time `sql: "index"`

    // Relationship
    Posts   *Posts `gorm: "PRELOAD:false"`
}

任何建议都会被采纳,而且我的围棋水平并没有我一个月前开始玩的那么好

不确定你想要达到什么,但GORM的回应离你需要的并不远

能否尝试向结构添加更多编码json标记,即:

  • 忽略“CreatedAt”字段
  • 将“用户”转换为“用户”(也适用于标记映射)
顺便说一句,还有json:,ommitempty“来忽略空字段


请提供更多信息,您的目标尚未明确,还需要更多信息?将一种数据结构转换为另一种是基本编程。你必须这么做,这里没有什么需要“知道”的。你读了我帖子的最后一行了吗?我说“我的围棋水平没有我一个月前开始玩的那么好”。你可以定义与所需json结构匹配的自定义结构,然后在检索gorm模型后,将这些模型转换为新的结构,这将在循环中手动执行,对象到对象,字段到字段。完成转换后,可以对结果进行json封送处理,就完成了。
    CreatedAt   time.Time `json:"-"` 
    User         *User `gorm: "foreignkey:UserRefer" json:"user"`