douxiegan6468
2015-12-29 19:28
浏览 200
已采纳

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

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.

图片转代码服务由CSDN问答提供 功能建议

我尝试将以下JSON数组转换为结构。 \ n

  [
 {
“ titel”:“测试1”,
“ event”:“某些值”,
“ pair”:“某些值”,
“条件”:  [
“或”,
 [
“包含”,
“ url”,
“ /” 
] 
],
“操作”:[
 [
“ option1”,  
“ 12”,
“ 1” 
],
 [
“ option2”,
“ 3”,
“ 1” 
] 
] 
},{
“ titel  “:”测试2“,
”事件“:”某些值“,
”对“:”某些值“,
”条件“:[
”或“,
 [
”包含“  ,
“ url”,
“ /” 
] 
],
“ actions”:[
 [
“ option1”,
“ 12”,
  “ 1” 
],
 [
“ option2”,
“ 3”,
“ 1” 
] 
] 
} 
] 
   \  n 
 

这是我到目前为止拥有的结构:

  type触发器结构{
事件字符串`json:“ event”`
对字符串 `json:“ pair”`
操作[] []字符串`json:“ actions”`
条件[] interface {}`json:“ condition”`
} 
 
type触发结构{
集合 [] Trigger 
} 
   
 
 

但是,这并没有真正涵盖“条件”部分。 理想情况下,id也希望具有一个结构。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtlzdofl66441 2015-12-29 21:45
    已采纳

    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
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题