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条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 Ubuntu20.04无法连接GitHub
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥30 C++行情软件的tick数据如何高效的合成K线