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

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

Create struct for complex JSON array in Golang

问题

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

[
    {
        "titel": "test 1",
        "event": "some value",
        "pair": "some value",
        "condition": [
            "or",
            [
                "contains",
                "url",
                "/"
            ]
        ],
        "actions": [
            [
                "option1",
                "12",
                "1"
            ],
            [
                "option2",
                "3",
                "1"
            ]
        ]
    },
    {
        "titel": "test 2",
        "event": "some value",
        "pair": "some value",
        "condition": [
            "or",
            [
                "contains",
                "url",
                "/"
            ]
        ],
        "actions": [
            [
                "option1",
                "12",
                "1"
            ],
            [
                "option2",
                "3",
                "1"
            ]
        ]
    }
]

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

type Trigger struct {
    Event     string        `json:"event"`	
    Pair      string        `json:"pair"`	
    Actions   [][]string    `json:"actions"`
    Condition []interface{} `json:"condition"`
}

type Triggers struct {
    Collection []Trigger
}

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

英文:

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

[
    {
        "titel": "test 1",
        "event": "some value",
        "pair": "some value",
        "condition": [
            "or",
            [
                "contains",
                "url",
                "/"
            ]
        ],
        "actions": [
            [
                "option1",
                "12",
                "1"
            ],
            [
                "option2",
                "3",
                "1"
            ]
        ]
    }, {
        "titel": "test 2",
        "event": "some value",
        "pair": "some value",
        "condition": [
            "or",
            [
                "contains",
                "url",
                "/"
            ]
        ],
        "actions": [
            [
                "option1",
                "12",
                "1"
            ],
            [
                "option2",
                "3",
                "1"
            ]
        ]
    }
]

This is the struct I've got so far:

type Trigger struct {
	Event     string        `json:"event"`	
	Pair      string        `json:"pair"`	
	Actions   [][]string    `json:"actions"`
	Condition []interface{} `json:"condition"`
}

type Triggers struct {
	Collection []Trigger
}

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

答案1

得分: 3

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

type Trigger struct {
    Event     string     `json:"event"`
    Pair      string     `json:"pair"`
    Actions   [][]string `json:"actions"`
    Condition Condition  `json:"condition"`
}

type Condition []interface{}

func (c *Condition) Typ() string {
    return (*c)[0].(string)
}

func (c *Condition) Val() []string {
    xs := (*c)[1].([]interface{})
    ys := make([]string, len(xs))
    for i, x := range xs {
        ys[i] = x.(string)
    }
    return ys
}

type Triggers struct {
    Collection []Trigger
}

你可以在这里查看完整的代码示例: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

type Trigger struct {
	Event     string     `json:"event"`
	Pair      string     `json:"pair"`
	Actions   [][]string `json:"actions"`
	Condition Condition  `json:"condition"`
}

type Condition []interface{}

func (c *Condition) Typ() string {
	return (*c)[0].(string)
}

func (c *Condition) Val() []string {
	xs := (*c)[1].([]interface{})
	ys := make([]string, len(xs))
	for i, x := range xs {
		ys[i] = x.(string)
	}
	return ys
}

type Triggers struct {
	Collection []Trigger
}

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:

确定