如何在Golang中为JSON字符串添加前缀

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

how to add prefix json string in golang

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是新手。我在我的数据库中有一个image,它以路径的形式存储。在API的JSON中,我需要输出这个路径,并在其前面添加一个prefix,我该如何做呢?

我的函数:

  1. func GetAllSliders(c *gin.Context) {
  2. var sliders models.Slider
  3. config.DB.Model(models.Slider{}).Where("id=?", 1).Update("image", ("https://spares-dt.ru/" + models.Slider{}.Image)) //我尝试了这个,但它不起作用
  4. if err := config.DB.Find(&sliders).Error; err != nil {
  5. c.JSON(http.StatusInternalServerError, err.Error())
  6. } else {
  7. c.JSON(http.StatusOK, gin.H{"data": sliders})
  8. }
  9. }

我有的JSON输出:

  1. {
  2. "data": {
  3. "id": 1,
  4. "main_text": "123",
  5. "upper_text": "123",
  6. "down_text": "123",
  7. "image": "data/photos/sliders/image.PNG"
  8. }
  9. }

我想要的输出:

  1. {
  2. "data": {
  3. "id": 1,
  4. "main_text": "123",
  5. "upper_text": "123",
  6. "down_text": "123",
  7. "image": "https://spakes-dt.ru/data/photos/sliders/image.PNG"
  8. }
  9. }

我的结构体:

  1. type Slider struct {
  2. Id uint `json:"id" gorm:"primaryKey"`
  3. MainText string `json:"main_text"`
  4. UpperText string `json:"upper_text"`
  5. DownText string `json:"down_text"`
  6. Image string `json:"image"`
  7. }
英文:

I am newbie. I have a image in my db which is stored as a path to it. In api json I have to output this path and add a prefix in front of it, how can I do that?
My function:

  1. func GetAllSliders(c *gin.Context) {
  2. var sliders models.Slider
  3. config.DB.Model(models.Slider{}).Where("id=?", 1).Update("image", ("https://spares-dt.ru/" + models.Slider{}.Image)) //i tried this, but it doesnt work
  4. if err := config.DB.Find(&sliders).Error; err != nil {
  5. c.JSON(http.StatusInternalServerError, err.Error())
  6. } else {
  7. c.JSON(http.StatusOK, gin.H{"data": sliders})
  8. }
  9. }

Json output i have:

  1. {
  2. "data": {
  3. "id": 1,
  4. "main_text": "123",
  5. "upper_text": "123",
  6. "down_text": "123",
  7. "image": "data/photos/sliders/image.PNG"
  8. }
  9. }

I want:

  1. {
  2. "data": {
  3. "id": 1,
  4. "main_text": "123",
  5. "upper_text": "123",
  6. "down_text": "123",
  7. "image": "https://spakes-dt.ru/data/photos/sliders/image.PNG"
  8. }
  9. }

And my struct:

  1. type Slider struct {
  2. Id uint `json:"id" gorm:"primaryKey"`
  3. MainText string `json:"main_text"`
  4. UpperText string `json:"upper_text"`
  5. DownText string `json:"down_text"`
  6. Image string `json:"image"`
  7. }

答案1

得分: 1

在JSON编组之前,只需更改Image字段的值:

  1. if err := config.DB.Find(&sliders).Error; err != nil {
  2. c.JSON(http.StatusInternalServerError, err.Error())
  3. } else {
  4. sliders.Image = "https://spares-dt.ru/" + sliders.Image
  5. c.JSON(http.StatusOK, gin.H{"data": sliders})
  6. }

在对sliders进行JSON编组之前,将Image字段的值修改为"https://spares-dt.ru/" + sliders.Image

英文:

Simply change the Image field's value before JSON marshaling it:

  1. if err := config.DB.Find(&sliders).Error; err != nil {
  2. c.JSON(http.StatusInternalServerError, err.Error())
  3. } else {
  4. sliders.Image = "https://spares-dt.ru/" + sliders.Image
  5. c.JSON(http.StatusOK, gin.H{"data": sliders})
  6. }

答案2

得分: 0

这是一个自定义JSON编组的示例。通过添加MarshalJson方法来修改您的模型,可以帮助您进行自定义。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Slider struct {
  7. ID int `json:"id"`
  8. MainText string `json:"main_text"`
  9. UpperText string `json:"upper_text"`
  10. DownText string `json:"down_text"`
  11. Image string `json:"image"`
  12. }
  13. // 添加MarshalJson方法
  14. func (u *Slider) MarshalJSON() ([]byte, error) {
  15. return json.Marshal(&struct {
  16. ID int `json:"id"`
  17. MainText string `json:"main_text"`
  18. UpperText string `json:"upper_text"`
  19. DownText string `json:"down_text"`
  20. Image string `json:"image"`
  21. }{
  22. ID: u.ID,
  23. MainText: u.MainText,
  24. UpperText: u.UpperText,
  25. DownText: u.DownText,
  26. Image: "https://spakes-dt.ru/" + u.Image,
  27. })
  28. }
  29. func main() {
  30. data := Slider{Image: "data/photos/sliders/image.PNG"}
  31. bdata, _ := json.Marshal(data)
  32. fmt.Println(string(bdata))
  33. }

这将产生以下输出。

  1. {
  2. "id": 0,
  3. "main_text": "",
  4. "upper_text": "",
  5. "down_text": "",
  6. "image": "data/photos/sliders/image.PNG"
  7. }

在创建MarshalJson方法后,更新您的代码如下所示。

  1. func GetAllSliders(c *gin.Context) {
  2. var sliders models.Slider
  3. config.DB.Model(models.Slider{}).Where("id=?", 1)//我尝试了这个,但它不起作用
  4. if err := config.DB.Find(&sliders).Error; err != nil {
  5. c.JSON(http.StatusInternalServerError, err.Error())
  6. } else {
  7. b, _ := json.Marshal(sliders)
  8. c.JSON(http.StatusOK, gin.H{"data": string(b)})
  9. }
  10. }
英文:

This is an example of how custom json marshalling works. Modifying your model by adding the MarshalJson method will help you to customize.

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Slider struct {
  7. ID int `json:"id"`
  8. MainText string `json:"main_text"`
  9. UpperText string `json:"upper_text"`
  10. DownText string `json:"down_text"`
  11. Image string `json:"image"`
  12. }
  13. //Add MarshalJson function
  14. func (u *Slider) MarshalJSON() ([]byte, error) {
  15. return json.Marshal(&struct {
  16. ID int `json:"id"`
  17. MainText string `json:"main_text"`
  18. UpperText string `json:"upper_text"`
  19. DownText string `json:"down_text"`
  20. Image string `json:"image"`
  21. }{
  22. ID: u.ID,
  23. MainText: u.MainText,
  24. UpperText: u.UpperText,
  25. DownText: u.DownText,
  26. Image: "https://spakes-dt.ru/" + u.Image,
  27. })
  28. }
  29. func main() {
  30. data := Slider{Image: "data/photos/sliders/image.PNG"}
  31. bdata, _ := json.Marshal(data)
  32. fmt.Println(string(bdata))
  33. }

This will produce the following output.

  1. {
  2. "id": 0,
  3. "main_text": "",
  4. "upper_text": "",
  5. "down_text": "",
  6. "image": "data/photos/sliders/image.PNG"
  7. }

Update your code something like this after you create MarshalJson method

  1. func GetAllSliders(c *gin.Context) {
  2. var sliders models.Slider
  3. config.DB.Model(models.Slider{}).Where("id=?", 1)//i tried this, but it doesnt work
  4. if err := config.DB.Find(&sliders).Error; err != nil {
  5. c.JSON(http.StatusInternalServerError, err.Error())
  6. } else {
  7. b, _ := json.Marshal(sliders)
  8. c.JSON(http.StatusOK, gin.H{"data": string(b)})
  9. }
  10. }

答案3

得分: 0

你应该使用exec函数:

  1. config.DB.Exec(`UPDATE slider SET image = "https://spares-dt.ru/" + image WHERE id = ?`, 1)
英文:

you should use exec

  1. config.DB.Exec(`UPDATE slider SET image = "https://spares-dt.ru/" + iamge WHERE id = ?`, 1)

huangapple
  • 本文由 发表于 2022年8月3日 17:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/73219156.html
匿名

发表评论

匿名网友

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

确定