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

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)