急!gorm的使用,预加载的问题

Lcode-0403 · 2023-03-14 02:40:08 · 1555 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2023-03-14 02:40:08 的主题,其中的信息可能已经有所发展或是发生改变。

{ "message":"success", "data":{ "data":[ { "id":44, "user":{ "id":22, "username":"Lcode-0403", "avatarUrl":"https://avatars.githubusercontent.com/u/58282852?v=4" }, "content":"博主为啥nuxt内存爆炸啊。啥问题", "status":1, "subComments":[ { "id":45, "user":{ "id":20, "username":"SuZiquan", "avatarUrl":"https://avatars.githubusercontent.com/u/7196781?v=4" }, "replyToUser":{ "id":22, "username":"Lcode-0403", "avatarUrl":"https://avatars.githubusercontent.com/u/58282852?v=4", "socialSource":"github", "lastLogin":"2023-03-13 12:47" }, "content":"Hello,这是一个服务端渲染(Server Side Render)的项目", "status":1, "parentId":44, "createdAt":"2023-03-03 14:59" } ], "createdAt":"2023-03-03 01:19" } ] } }

我想响应类似上面格式的json,这是一个java处理返回的结果,我想用go重写。下面是我的数据表 微信截图_20230314023055.png

我自己是写了这样的一个结构体,这样的结构体预加载出不来一样的结构,而且还有个subComments字段没有思路。 微信截图_20230314023231.png

😭😭😭😭😭😭


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1555 次点击  
加入收藏 微博
3 回复  |  直到 2023-03-14 14:43:03
Lcode-0403
Lcode-0403 · #1 · 2年之前

json太长了,复制再去解析会看得比较清楚😉

buguang01
buguang01 · #2 · 2年之前

根结构是JSONData

type JSONData struct {
    Message string `json:"message"`
    Data DataArray `json:"data"`
}
type User struct {
    ID int `json:"id"`
    Username string `json:"username"`
    AvatarURL string `json:"avatarUrl"`
}
type ReplyToUser struct {
    ID int `json:"id"`
    Username string `json:"username"`
    AvatarURL string `json:"avatarUrl"`
    SocialSource string `json:"socialSource"`
    LastLogin string `json:"lastLogin"`
}
type SubComments struct {
    ID int `json:"id"`
    User User `json:"user"`
    ReplyToUser ReplyToUser `json:"replyToUser"`
    Content string `json:"content"`
    Status int `json:"status"`
    ParentID int `json:"parentId"`
    CreatedAt string `json:"createdAt"`
}
type Data struct {
    ID int `json:"id"`
    User User `json:"user"`
    Content string `json:"content"`
    Status int `json:"status"`
    SubComments []SubComments `json:"subComments"`
    CreatedAt string `json:"createdAt"`
}
type DataArray struct {
    Data []Data `json:"data"`
}
Lcode-0403
Lcode-0403 · #3 · 2年之前
buguang01buguang01 #2 回复

根结构是JSONData ```go type JSONData struct { Message string `json:"message"` Data DataArray `json:"data"` } type User struct { ID int `json:"id"` Username string `json:"username"` AvatarURL string `json:"avatarUrl"` } type ReplyToUser struct { ID int `json:"id"` Username string `json:"username"` AvatarURL string `json:"avatarUrl"` SocialSource string `json:"socialSource"` LastLogin string `json:"lastLogin"` } type SubComments struct { ID int `json:"id"` User User `json:"user"` ReplyToUser ReplyToUser `json:"replyToUser"` Content string `json:"content"` Status int `json:"status"` ParentID int `json:"parentId"` CreatedAt string `json:"createdAt"` } type Data struct { ID int `json:"id"` User User `json:"user"` Content string `json:"content"` Status int `json:"status"` SubComments []SubComments `json:"subComments"` CreatedAt string `json:"createdAt"` } type DataArray struct { Data []Data `json:"data"` } ```

终于解决了👍。不过还是说一句gorm真的复杂

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传