将 JSON 数组解析为 Golang 的结构体列表。

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

Parse json array into list of struct golang

问题

我有一个如下所示的 JSON,我正在尝试为该 JSON 创建一个结构体,以便在解组时可以存储数据。

  1. {
  2. "clientMetrics": [
  3. {
  4. "clientId": 951231,
  5. "customerData": {
  6. "Process": [
  7. "ABC"
  8. ],
  9. "Mat": [
  10. "KKK"
  11. ]
  12. },
  13. "legCustomer": [
  14. 8773
  15. ]
  16. },
  17. {
  18. "clientId": 1234,
  19. "legCustomer": [
  20. 8789
  21. ]
  22. },
  23. {
  24. "clientId": 3435,
  25. "otherIds": [
  26. 4,
  27. 32,
  28. 19
  29. ],
  30. "legCustomer": [
  31. 10005
  32. ]
  33. },
  34. {
  35. "clientId": 9981,
  36. "catId": 8,
  37. "legCustomer": [
  38. 13769
  39. ]
  40. },
  41. {
  42. "clientId": 12124,
  43. "otherIds": [
  44. 33,
  45. 29
  46. ],
  47. "legCustomer": [
  48. 12815
  49. ]
  50. },
  51. {
  52. "clientId": 8712,
  53. "customerData": {
  54. "Process": [
  55. "College"
  56. ]
  57. },
  58. "legCustomer": [
  59. 951
  60. ]
  61. },
  62. {
  63. "clientId": 23214,
  64. "legCustomer": [
  65. 12724,
  66. 12727
  67. ]
  68. },
  69. {
  70. "clientId": 119812,
  71. "catId": 8,
  72. "legCustomer": [
  73. 14519
  74. ]
  75. },
  76. {
  77. "clientId": 22315,
  78. "otherIds": [
  79. 32
  80. ],
  81. "legCustomer": [
  82. 12725,
  83. 13993
  84. ]
  85. },
  86. {
  87. "clientId": 765121,
  88. "catId": 8,
  89. "legCustomer": [
  90. 14523
  91. ]
  92. }
  93. ]
  94. }

clientMetrics 是一个包含每个 clientMetric 对象的 JSON 数组。每个 clientMetric 对象可以具有各种字段。我尝试了以下代码,但是我对如何添加剩余部分感到困惑,因为我来自 Java 背景,而在 Golang 中似乎没有可用的 set。我还不确定如何添加 customerData 对象。

  1. type ClientMetrics struct {
  2. ClientId int64
  3. CatId int64
  4. // 这里应该添加其他字段
  5. }

在 Golang 中,将上述 JSON 解组为 ClientMetrics 结构体列表的最佳方法是什么?

英文:

I have a json like below and I am trying to make a struct for below json which can store the data for me when I unmarshall it.

  1. {
  2. "clientMetrics": [
  3. {
  4. "clientId": 951231,
  5. "customerData": {
  6. "Process": [
  7. "ABC"
  8. ],
  9. "Mat": [
  10. "KKK"
  11. ]
  12. },
  13. "legCustomer": [
  14. 8773
  15. ]
  16. },
  17. {
  18. "clientId": 1234,
  19. "legCustomer": [
  20. 8789
  21. ]
  22. },
  23. {
  24. "clientId": 3435,
  25. "otherIds": [
  26. 4,
  27. 32,
  28. 19
  29. ],
  30. "legCustomer": [
  31. 10005
  32. ]
  33. },
  34. {
  35. "clientId": 9981,
  36. "catId": 8,
  37. "legCustomer": [
  38. 13769
  39. ]
  40. },
  41. {
  42. "clientId": 12124,
  43. "otherIds": [
  44. 33,
  45. 29
  46. ],
  47. "legCustomer": [
  48. 12815
  49. ]
  50. },
  51. {
  52. "clientId": 8712,
  53. "customerData": {
  54. "Process": [
  55. "College"
  56. ]
  57. },
  58. "legCustomer": [
  59. 951
  60. ]
  61. },
  62. {
  63. "clientId": 23214,
  64. "legCustomer": [
  65. 12724,
  66. 12727
  67. ]
  68. },
  69. {
  70. "clientId": 119812,
  71. "catId": 8,
  72. "legCustomer": [
  73. 14519
  74. ]
  75. },
  76. {
  77. "clientId": 22315,
  78. "otherIds": [
  79. 32
  80. ],
  81. "legCustomer": [
  82. 12725,
  83. 13993
  84. ]
  85. },
  86. {
  87. "clientId": 765121,
  88. "catId": 8,
  89. "legCustomer": [
  90. 14523
  91. ]
  92. }
  93. ]
  94. }

clientMetrics is a json array which contains each clientMetric object. Each clientMetric object can have various fields into it. I tried something like below but I am confuse on how to add rest since I am coming from Java background and I don't see there is set available in golang. Also confuse on how to add customerData object too.

  1. type ClientMetrics struct {
  2. ClientId int64
  3. CatId int64
  4. }

What is the best way to unmarshall above json into a list of ClientMetrics struct in golang?

答案1

得分: 2

你可以在这里使用json to go:https://mholt.github.io/json-to-go/

但是它会重复两次CustomerData结构体,确保你应该删除其中一个。

我已经为你的场景创建了一个示例结构体,如下所示:

  1. type AutoGenerated struct {
  2. ClientMetrics []struct {
  3. ClientID int `json:"clientId"`
  4. CustomerData struct {
  5. Process []string `json:"Process"`
  6. Mat []string `json:"Mat"`
  7. } `json:"customerData,omitempty"`
  8. LegCustomer []int `json:"legCustomer"`
  9. OtherIds []int `json:"otherIds,omitempty"`
  10. CatID int `json:"catId,omitempty"`
  11. } `json:"clientMetrics"`
  12. }

你可以在go playground上运行它:https://go.dev/play/p/R1M1HfzpEny

英文:

You can use json to go here : https://mholt.github.io/json-to-go/

But it will repeat CustomerData struct twice make sure you should remove one of them.

I have created a sample struct for your scenario as follows :

  1. type AutoGenerated struct {
  2. ClientMetrics []struct {
  3. ClientID int `json:"clientId"`
  4. CustomerData struct {
  5. Process []string `json:"Process"`
  6. Mat []string `json:"Mat"`
  7. } `json:"customerData,omitempty"`
  8. LegCustomer []int `json:"legCustomer"`
  9. OtherIds []int `json:"otherIds,omitempty"`
  10. CatID int `json:"catId,omitempty"`
  11. } `json:"clientMetrics"`
  12. }

You can run it here in go playground : https://go.dev/play/p/R1M1HfzpEny

答案2

得分: 1

如果你正在使用VS Code,有几个扩展可以完成这个任务。其中一个扩展名为Paste JSON as Code

  1. 安装这个扩展
  2. 复制JSON到你的剪贴板中(Ctrl+C)
  3. 按下Ctrl+Shift+P,选择Paste JSON as code
  4. 输入结构体的名称,然后按回车键

如果这对你不起作用,你可以使用这个网站https://mholt.github.io/json-to-go/,但最好的做法是在取消选择Inline type definitions选项后使用获得的结构体。

英文:

If you're using VS Code, there are a few extensions that can do this job.
One of them is named Paste JSON as Code.

  1. Install the extension
  2. Copy the JSON and in your clipboard (ctrl+c)
  3. Press Ctrl+Shift+P and select Paste JSON as code
  4. Type name of struct and press enter

If this doesn't work for you, you can always use this site https://mholt.github.io/json-to-go/ but it would be a better practice to use the struct obtained after unselecting Inline type definitions option.

huangapple
  • 本文由 发表于 2021年12月29日 15:55:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/70516591.html
匿名

发表评论

匿名网友

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

确定