第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為什么不能將此數(shù)據(jù)正確解組到我的對象模型中?

為什么不能將此數(shù)據(jù)正確解組到我的對象模型中?

Go
九州編程 2023-04-17 15:59:14
我這里有一個(gè)(非)工作示例:https ://play.golang.org/p/qaYhKvJ65J3我不確定為什么會(huì)出現(xiàn)以下數(shù)據(jù):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": []    }}`我只是得到:json object:::: {Evaluator:{Params:[] Type:} Operator:{Type:} Query:{Params:[]} Reducer:{Params:[] Type:} Type:}理想情況下,我能夠?qū)⑵浣馕鰹轭愃频臇|西type Conditions []struct{ },但我不確定您是否可以將模型定義為列表?
查看完整描述

2 回答

?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊

看起來您正在嘗試訪問嵌套在根“設(shè)置”屬性下的“條件”屬性。因此,您需要定義該根級(jí)類型和足夠的字段來告訴解組器如何找到您的目標(biāo)屬性。因此,您只需要?jiǎng)?chuàng)建一個(gè)帶有必要的“設(shè)置/條件”字段的新“AlertData”類型。

例如:

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\n", 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"}}

}

請注意,打印的列表包含如此多的類型信息,因?yàn)椤皸l件”類型使用匿名結(jié)構(gòu)作為字段類型。如果您要將它們提取到命名結(jié)構(gòu)中,那么使用數(shù)據(jù)會(huì)更容易,例如:


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",

//? ?},

// }


查看完整回答
反對 回復(fù) 2023-04-17
?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊

這是一種替代方法,它圍繞結(jié)構(gòu)方法進(jìn)行訪問,數(shù)據(jù)結(jié)構(gòu)也已完全定義。

package main


import (

? ? "encoding/json"

? ? "fmt"

? ? "log"

? ? "time"

)


type Data struct {

? ? ID? ? ? ? ? ? ?int? ? ? ? ?`json:"Id"`

? ? Version? ? ? ? int? ? ? ? ?`json:"Version"`

? ? OrgID? ? ? ? ? int? ? ? ? ?`json:"OrgId"`

? ? DashboardID? ? int? ? ? ? ?`json:"DashboardId"`

? ? PanelID? ? ? ? int? ? ? ? ?`json:"PanelId"`

? ? Name? ? ? ? ? ?string? ? ? `json:"Name"`

? ? Message? ? ? ? string? ? ? `json:"Message"`

? ? Severity? ? ? ?string? ? ? `json:"Severity"`

? ? State? ? ? ? ? string? ? ? `json:"State"`

? ? Handler? ? ? ? int? ? ? ? ?`json:"Handler"`

? ? Silenced? ? ? ?bool? ? ? ? `json:"Silenced"`

? ? ExecutionError string? ? ? `json:"ExecutionError"`

? ? Frequency? ? ? int? ? ? ? ?`json:"Frequency"`

? ? EvalData? ? ? ?interface{} `json:"EvalData"`

? ? NewStateDate? ?time.Time? ?`json:"NewStateDate"`

? ? PrevStateDate? time.Time? ?`json:"PrevStateDate"`

? ? StateChanges? ?int? ? ? ? ?`json:"StateChanges"`

? ? Created? ? ? ? time.Time? ?`json:"Created"`

? ? Updated? ? ? ? time.Time? ?`json:"Updated"`

? ? Settings? ? ? ?struct {

? ? ? ? Conditions? ? ? ? ? []Condition? ?`json:"conditions"`

? ? ? ? ExecutionErrorState string? ? ? ? `json:"executionErrorState"`

? ? ? ? Frequency? ? ? ? ? ?string? ? ? ? `json:"frequency"`

? ? ? ? Handler? ? ? ? ? ? ?int? ? ? ? ? ?`json:"handler"`

? ? ? ? Name? ? ? ? ? ? ? ? string? ? ? ? `json:"name"`

? ? ? ? NoDataState? ? ? ? ?string? ? ? ? `json:"noDataState"`

? ? ? ? Notifications? ? ? ?[]interface{} `json:"notifications"`

? ? } `json:"Settings"`

}


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 {

? ? ? ? DatasourceID int `json:"datasourceId"`

? ? ? ? Model? ? ? ? struct {

? ? ? ? ? ? Hide? ? ? ?bool? ?`json:"hide"`

? ? ? ? ? ? RefCount? ?int? ? `json:"refCount"`

? ? ? ? ? ? RefID? ? ? string `json:"refId"`

? ? ? ? ? ? TextEditor bool? ?`json:"textEditor"`

? ? ? ? } `json:"model"`

? ? ? ? Params []string `json:"params"`

? ? } `json:"query"`

? ? Reducer struct {

? ? ? ? Params []interface{} `json:"params"`

? ? ? ? Type? ?string? ? ? ? `json:"type"`

? ? } `json:"reducer"`

? ? Type string `json:"type"`

}


func (d Data) GetFirstCondition() (Condition, error) {

? ? if len(d.Settings.Conditions) > 0 {

? ? ? ? return d.Settings.Conditions[0], nil

? ? }

? ? return Condition{}, fmt.Errorf("no conditions found")

}


func (d Data) GetConditionByIndex(index uint) (Condition, error) {

? ? if len(d.Settings.Conditions) == 0 {

? ? ? ? return Condition{}, fmt.Errorf("no conditions found")

? ? }


? ? if int(index) > len(d.Settings.Conditions)-1 {

? ? ? ? return Condition{}, fmt.Errorf("index out of bounds")

? ? }


? ? return d.Settings.Conditions[index], nil

}


var 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": []

? ? }

}`


func main() {

? ? var res Data

? ? err := json.Unmarshal([]byte(alertData), &res)

? ? if err != nil {

? ? ? ? log.Fatal(err)

? ? }

? ? fmt.Println(res.GetFirstCondition())

? ? fmt.Println(res.GetConditionByIndex(0))

? ? // should fail :-)

? ? fmt.Println(res.GetConditionByIndex(1))


}


查看完整回答
反對 回復(fù) 2023-04-17
  • 2 回答
  • 0 關(guān)注
  • 133 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)