如何在 Golang 中压缩 JSON 数据结构?

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

how to compact Json in Struct Golang

问题

包问卷

导入 (
"encoding/json"
)

类型项目[]项目

类型CreateData struct {
项目[]项目
}

类型项目 struct {
Id string json:"id" required:"true"
CompCd string json:"compCd" required:"true"
OrgCd string json:"orgCd"
QstnId string json:"qstnId" required:"true"
QstnIdSeq string json:"qstnIdSeq" required:"true"
CustId string json:"custId"
AnsDts string json:"ansDts" required:"true"
AnsRout string json:"ansRout" required:"true"
QCd01 string json:"qCd01"
QKey01 string json:"qKey01"
QStc01 string json:"qStc01"
QCat01 string json:"qCat01"
Pont01 string json:"pont01"
PCat01 string json:"pCat01"
Comt01 string json:"comt01"
QCd02 string json:"qCd02"
QKey02 string json:"qKey02"
QStc02 string json:"qStc02"
QCat02 string json:"qCat02"
Pont02 string json:"pont02"
PCat02 string json:"pCat02"
Comt02 string json:"comt02"
.
.
.
QCd50 string json:"qCd50"
QKey50 string json:"qKey50"
QStc50 string json:"qStc50"
QCat50 string json:"qCat50"
Pont50 string json:"pont50"
PCat50 string json:"pCat50"
Comt50 string json:"comt50"
}
我是新手。我几天前才开始学习Go语言。
我的问题是:如何简化这个项目结构块。如果我从QCD01写到QCD50,这段代码可以运行,但很糟糕!
对不起,我不擅长英语。

英文:
  1. package questionnaire
  2. import (
  3. "encoding/json"
  4. )
  5. type Items []Item
  6. type CreateData struct {
  7. Items []Item
  8. }
  9. type Item struct {
  10. Id enter code herestring `json:"id" required:"true"`
  11. CompCd string `json:"compCd" required:"true"`
  12. OrgCd string `json:"orgCd"`
  13. QstnId string `json:"qstnId" required:"true"`
  14. QstnIdSeq string `json:"qstnIdSeq" required:"true"`
  15. CustId string `json:"custId"`
  16. AnsDts string `json:"ansDts" required:"true"`
  17. AnsRout string `json:"ansRout" required:"true"`
  18. QCd01 string `json:"qCd01"`
  19. QKey01 string `json:"qKey01"`
  20. QStc01 string `json:"qStc01"`
  21. QCat01 string `json:"qCat01"`
  22. Pont01 string `json:"pont01"`
  23. PCat01 string `json:"pCat01"`
  24. Comt01 string `json:"comt01"`
  25. QCd02 string `json:"qCd02"`
  26. QKey02 string `json:"qKey02"`
  27. QStc02 string `json:"qStc02"`
  28. QCat02 string `json:"qCat02"`
  29. Pont02 string `json:"pont02"`
  30. PCat02 string `json:"pCat02"`
  31. Comt02 string `json:"comt02"`
  32. .
  33. .
  34. .
  35. QCd50 string `json:"qCd50"`
  36. QKey50 string `json:"qKey50"`
  37. QStc50 string `json:"qStc50"`
  38. QCat50 string `json:"qCat50"`
  39. Pont50 string `json:"pont50"`
  40. PCat50 string `json:"pCat50"`
  41. Comt50 string `json:"comt50"
  42. }

I'm newbie. I just learn go language a few day before..
My problem: How to compact this Item struct block. If i write from QCd01 to QCd50 this code can run but so bad!
Sorry I'm not good at English.

答案1

得分: 0

我复制并粘贴了你评论中的代码。它缺少结束的 } 来完成 json 字符串。此外,你需要一个结构体来将 items 映射到。这是一个可工作的 playground 链接。

编辑:我没有意识到会有多个 "items",鉴于名字是复数形式,我应该假设会有多个。这是一个更新后的 playground 链接,解决了这个问题。

playground链接

英文:

I copied and pasted the code in your comment. It was missing the ending } to finish the json string. Also, you need a struct to tie the items map to. Here is a working playground link.

EDIT: I didn't realize there would be more than one "items", which I should have assumed given the plural name. Here is an updated playground link that takes care of that.

https://play.golang.org/p/5T2C6KrVOD

huangapple
  • 本文由 发表于 2017年2月19日 01:36:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/42318448.html
匿名

发表评论

匿名网友

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

确定