我有以下 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}但是,这并没有真正涵盖“条件”部分。理想情况下,id 也喜欢有一个结构。
1 回答
交互式爱情
TA贡献1712条经验 获得超3个赞
假设根数组中的每个项目只能有一个条件,您可以尝试下面的结构。这可以使使用Condition清楚。
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
}
- 1 回答
- 0 关注
- 186 浏览
添加回答
举报
0/150
提交
取消