douchen7366 2018-10-02 21:44
浏览 37
已采纳

为什么不能将这些数据正确地解组到我的对象模型中?

I have a (non)working example here: https://play.golang.org/p/qaYhKvJ65J3

I'm not sure why the following data:

alertData := `{
    "Id": 0,
    "Version": 0,
    "OrgId": 1,
    "DashboardId": 61,
    "PanelId": 84,
    "Name": "{qa-dev}{stats-pipeline} Topology Message Age (aggregator) alert",
    "Message": "",
    "Severity": "",
    "State": "",
    "Handler": 1,
    "Silenced": false,
    "ExecutionError": "",
    "Frequency": 10,
    "EvalData": null,
    "NewStateDate": "0001-01-01T00:00:00Z",
    "PrevStateDate": "0001-01-01T00:00:00Z",
    "StateChanges": 0,
    "Created": "0001-01-01T00:00:00Z",
    "Updated": "0001-01-01T00:00:00Z",
    "Settings": {
        "conditions": [
            {
                "evaluator": {
                    "params": [
                        10000
                    ],
                    "type": "gt"
                },
                "operator": {
                    "type": "and"
                },
                "query": {
                    "datasourceId": 2,
                    "model": {
                        "hide": true,
                        "refCount": 0,
                        "refId": "C",
                        "textEditor": false
                    },
                    "params": [
                        "C",
                        "5m",
                        "now"
                    ]
                },
                "reducer": {
                    "params": [],
                    "type": "avg"
                },
                "type": "query"
            }
        ],
        "executionErrorState": "keep_state",
        "frequency": "10s",
        "handler": 1,
        "name": "{qa-dev}{stats-pipeline} Topology Message Age (aggregator) alert",
        "noDataState": "keep_state",
        "notifications": []
    }
}`

Can't be unmarshalled into the following object model:

type Condition struct {
    Evaluator struct {
        Params []int  `json:"params"`
        Type   string `json:"type"`
    } `json:"evaluator"`
    Operator struct {
        Type string `json:"type"`
    } `json:"operator"`
    Query struct {
        Params []string `json:"params"`
    } `json:"query"`
    Reducer struct {
        Params []interface{} `json:"params"`
        Type   string        `json:"type"`
    } `json:"reducer"`
    Type string `json:"type"`
}

When I do the following:

condition := Condition{}
err := json.Unmarshal([]byte(alertData), &condition)

if err != nil {
    panic(err)
}

fmt.Printf("

 json object:::: %+v", condition)

I just get: json object:::: {Evaluator:{Params:[] Type:} Operator:{Type:} Query:{Params:[]} Reducer:{Params:[] Type:} Type:}

Ideally I'd be able to parse it into something like type Conditions []struct{ } but I'm not sure if you can define models as lists?

  • 写回答

2条回答 默认 最新

  • dony113407 2018-10-02 22:07
    关注

    It looks like you are trying to access the "conditions" property nested under the root "Settings" property. As such, you need to define that root-level type and enough fields to tell the unmarshaler how to find your target property. As such, you would just need to create a new "AlertData" type with the necessary "Settings/conditions" fields.

    For example (Go Playground):

    type AlertData struct {
      Settings struct {
        Conditions []Condition `json:"conditions"`
      }
    }
    
    func main() {
      alert := AlertData{}
      err := json.Unmarshal([]byte(alertData), &alert)
    
      if err != nil {
        panic(err)
      }
    
      fmt.Printf("OK: conditions=%#v
    ", alert.Settings.Conditions)
      // OK: conditions=[]main.Condition{main.Condition{Evaluator:struct { Params []int "json:\"params\""; Type string "json:\"type\"" }{Params:[]int{10000}, Type:"gt"}, Operator:struct { Type string "json:\"type\"" }{Type:"and"}, Query:struct { Params []string "json:\"params\"" }{Params:[]string{"C", "5m", "now"}}, Reducer:struct { Params []interface {} "json:\"params\""; Type string "json:\"type\"" }{Params:[]interface {}{}, Type:"avg"}, Type:"query"}}
    }
    

    Note that the printed listing includes so much type information because the "Condition" type uses anonymous structs as field types. If you were to extract them into named structs it will be easier to work with the data, e.g.:

    type Condition struct {
      Evaluator Evaluator `json:"evaluator"`
      Operator  Operator  `json:"operator"`
      // ...
    }
    
    type Evaluator struct {
      Params []int  `json:"params"`
      Type   string `json:"type"`
    }
    
    type Operator struct {
      Type string `json:"type"`
    }
    
    //...
    // OK: conditions=[]main.Condition{
    //   main.Condition{
    //     Evaluator:main.Evaluator{Params:[]int{10000}, Type:"gt"},
    //     Operator:main.Operator{Type:"and"},
    //     Query:main.Query{Params:[]string{"C", "5m", "now"}},
    //     Reducer:main.Reducer{Params:[]interface {}{}, Type:"avg"},
    //     Type:"query",
    //   },
    // }
    

    Go Playground example here...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!