json:无法将对象解组为jsonpatch类型的Go值,Patch -如何修复

dffbzjpn  于 2023-10-14  发布在  Go
关注(0)|答案(1)|浏览(250)
  1. import (
  2. "database/sql"
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. jsonpatch "github.com/evanphx/json-patch"
  8. "github.com/gin-gonic/gin"
  9. "github.com/jasonalansmith/maelstrom-platform-api/database"
  10. )
  11. type Issue struct {
  12. SysId uint `json:"sysid,omitempty"`
  13. Identifier string `json:"identifier,omitempty"`
  14. SummaryBrief string `json:"summarybrief,omitempty"`
  15. SummaryLong string `json:"summarylong,omitempty"`
  16. }
  17. func patchIssue(ctx *gin.Context) {
  18. id := ctx.Param("sysid")
  19. // body := Issue{}
  20. //data, err := ctx.GetRawData()
  21. //if err != nil {
  22. // ctx.AbortWithStatusJSON(400, "Issue not defined.")
  23. // return
  24. //}
  25. //err = json.Unmarshal(data, &body)
  26. //if err != nil {
  27. // fmt.Println(err)
  28. // ctx.AbortWithStatusJSON(400, "Bad input.")
  29. // return
  30. //}
  31. iss := Issue{}
  32. sqls := "SELECT * FROM issue WHERE sysid = $1"
  33. res := database.Db.QueryRow(sqls, id)
  34. err := res.Scan(&iss.SysId, &iss.Identifier, &iss.SummaryBrief,
  35. &iss.SummaryLong)
  36. if err == sql.ErrNoRows {
  37. ctx.AbortWithStatusJSON(400, "Issue does not exist.")
  38. return
  39. }
  40. issueBytes, err := json.Marshal(iss)
  41. if err != nil {
  42. fmt.Println("Error creating patch json ", err.Error())
  43. return
  44. }
  45. patchJSON, err := io.ReadAll(ctx.Request.Body)
  46. fmt.Println(string(patchJSON))
  47. if err != nil {
  48. fmt.Println(err)
  49. }
  50. patch, err := jsonpatch.DecodePatch(patchJSON)
  51. if err != nil {
  52. fmt.Println("Error decoding patch json ", err.Error())
  53. return
  54. }
  55. patchedIssue, err := patch.Apply(issueBytes)
  56. if err != nil {
  57. fmt.Println("Error applying patch json ", err.Error())
  58. return
  59. }
  60. fmt.Println(patchedIssue)
  61. }

使用Go版本:
go版本go1.21.1 Linux/amd 64
当我运行这段代码时,我得到以下错误:
json:无法将对象解组为jsonpatch类型的Go值。Patch
我从here中改编了这段代码。
当我这样做:
fmt.Println(string(patchJSON))
我得到了看起来有效的JSON。
冒犯的一行是:
patch,err:= jsonpatch.DecodePatch(patchJSON)
我在openSuse Tumbleweed上使用curl来调用API:
curl -d '{“op”:“replace”,“path”:“/summarybrief”,“value”:“第二个测试问题”-X PATCH http://localhost:8080/issue/22-H“内容类型:application/json”
我做错了什么,得到这个错误?谢谢你,谢谢

wixjitnu

wixjitnu1#

{"op": "replace", "path": "/summarybrief", "value": "Second Test Issue"}不是有效的修补程序。根据the spec
JSON Patch文档是表示对象数组的JSON [RFC 4627]文档。
因此,补丁需要是一个数组-例如。[{"op": "replace", "path": "/summarybrief", "value": "Second Test Issue"}]将按照以下示例(playground)工作:

  1. package main
  2. import (
  3. "fmt"
  4. jsonpatch "github.com/evanphx/json-patch/v5"
  5. )
  6. func main() {
  7. // patchJSON := []byte(`{"op": "replace", "path": "/summarybrief", "value": "Second Test Issue"}`) // Fails with "Error decoding patch json json: cannot unmarshal object into Go value of type jsonpatch.Patch"
  8. patchJSON := []byte(`[{"op": "replace", "path": "/summarybrief", "value": "Second Test Issue"}]`) // Successfully processed
  9. patch, err := jsonpatch.DecodePatch(patchJSON)
  10. if err != nil {
  11. fmt.Println("Error decoding patch json ", err.Error())
  12. return
  13. }
  14. fmt.Println(patch)
  15. }
展开查看全部

相关问题