在Golang中创建复杂JSON数组的结构体。

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

Create struct for complex JSON array in Golang

问题

我有以下的JSON数组,我想要将其转换为一个结构体。

  1. [
  2. {
  3. "titel": "test 1",
  4. "event": "some value",
  5. "pair": "some value",
  6. "condition": [
  7. "or",
  8. [
  9. "contains",
  10. "url",
  11. "/"
  12. ]
  13. ],
  14. "actions": [
  15. [
  16. "option1",
  17. "12",
  18. "1"
  19. ],
  20. [
  21. "option2",
  22. "3",
  23. "1"
  24. ]
  25. ]
  26. },
  27. {
  28. "titel": "test 2",
  29. "event": "some value",
  30. "pair": "some value",
  31. "condition": [
  32. "or",
  33. [
  34. "contains",
  35. "url",
  36. "/"
  37. ]
  38. ],
  39. "actions": [
  40. [
  41. "option1",
  42. "12",
  43. "1"
  44. ],
  45. [
  46. "option2",
  47. "3",
  48. "1"
  49. ]
  50. ]
  51. }
  52. ]

这是我目前得到的结构体:

  1. type Trigger struct {
  2. Event string `json:"event"`
  3. Pair string `json:"pair"`
  4. Actions [][]string `json:"actions"`
  5. Condition []interface{} `json:"condition"`
  6. }
  7. type Triggers struct {
  8. Collection []Trigger
  9. }

然而,这并没有完全涵盖"Condition"部分。理想情况下,我也想为它定义一个结构体。

英文:

I have the following JSON array that I'm trying to convert to a struct.

  1. [
  2. {
  3. "titel": "test 1",
  4. "event": "some value",
  5. "pair": "some value",
  6. "condition": [
  7. "or",
  8. [
  9. "contains",
  10. "url",
  11. "/"
  12. ]
  13. ],
  14. "actions": [
  15. [
  16. "option1",
  17. "12",
  18. "1"
  19. ],
  20. [
  21. "option2",
  22. "3",
  23. "1"
  24. ]
  25. ]
  26. }, {
  27. "titel": "test 2",
  28. "event": "some value",
  29. "pair": "some value",
  30. "condition": [
  31. "or",
  32. [
  33. "contains",
  34. "url",
  35. "/"
  36. ]
  37. ],
  38. "actions": [
  39. [
  40. "option1",
  41. "12",
  42. "1"
  43. ],
  44. [
  45. "option2",
  46. "3",
  47. "1"
  48. ]
  49. ]
  50. }
  51. ]

This is the struct I've got so far:

  1. type Trigger struct {
  2. Event string `json:"event"`
  3. Pair string `json:"pair"`
  4. Actions [][]string `json:"actions"`
  5. Condition []interface{} `json:"condition"`
  6. }
  7. type Triggers struct {
  8. Collection []Trigger
  9. }

However, this does not really cover the "Condition" part. Ideally id like to have a structure for that as well.

答案1

得分: 3

假设根数组中的每个项目只能有一个条件,你可以尝试使用下面的结构体来实现。这样可以清晰地使用Condition

  1. type Trigger struct {
  2. Event string `json:"event"`
  3. Pair string `json:"pair"`
  4. Actions [][]string `json:"actions"`
  5. Condition Condition `json:"condition"`
  6. }
  7. type Condition []interface{}
  8. func (c *Condition) Typ() string {
  9. return (*c)[0].(string)
  10. }
  11. func (c *Condition) Val() []string {
  12. xs := (*c)[1].([]interface{})
  13. ys := make([]string, len(xs))
  14. for i, x := range xs {
  15. ys[i] = x.(string)
  16. }
  17. return ys
  18. }
  19. type Triggers struct {
  20. Collection []Trigger
  21. }

你可以在这里查看完整的代码示例:https://play.golang.org/p/WxFhBjJmEN

英文:

Assuming that there can be only a single condition per item in the root array, you can try a struct below. This could make using Condition clear.

https://play.golang.org/p/WxFhBjJmEN

  1. type Trigger struct {
  2. Event string `json:"event"`
  3. Pair string `json:"pair"`
  4. Actions [][]string `json:"actions"`
  5. Condition Condition `json:"condition"`
  6. }
  7. type Condition []interface{}
  8. func (c *Condition) Typ() string {
  9. return (*c)[0].(string)
  10. }
  11. func (c *Condition) Val() []string {
  12. xs := (*c)[1].([]interface{})
  13. ys := make([]string, len(xs))
  14. for i, x := range xs {
  15. ys[i] = x.(string)
  16. }
  17. return ys
  18. }
  19. type Triggers struct {
  20. Collection []Trigger
  21. }

huangapple
  • 本文由 发表于 2015年12月30日 03:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/34517713.html
匿名

发表评论

匿名网友

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

确定