亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將具有 json 的字符串轉換為 json 或 struct

將具有 json 的字符串轉換為 json 或 struct

Go
米琪卡哇伊 2023-07-31 15:38:17
我從 API 收到此類響應:{    "ok": true,    "response": "[    {        "Id": 163,        "Name": "Availability",        "Path": "Performance|Tier1",        "frequency": "ONE_MIN",        "Values": [            {                "startTimeInMillis": 1571314200000,                "occurrences": 1,                "current": 1,                "min": 0,                "max": 0,                "useRange": false,                "count": 1,                "sum": 1,                "value": 1,                "standardDeviation": 0            },            {                "startTimeInMillis": 1571314260000,                "occurrences": 1,                "current": 1,                "min": 0,                "max": 0,                "useRange": false,                "count": 1,                "sum": 1,                "value": 1,                "standardDeviation": 0            },            }       ]    }]}我想將其轉換為時間序列格式。為此,我首先嘗試解組對此結構的響應:type App struct{    ID     string   `json:"metric_id"`    Name   string   `json:"metric_name"`    Path   string   `json:"metric_path"`    Frequency    string   `json:"frequency"`    Values []string `json:"metric_values"`} 我正在這樣做:apprsp := App{}fmt.Println(json.Unmarshal([]byte(ame.Response), &apprsp))但我在 時遇到錯誤json.Unmarshal。我想做的是生成一個 json 格式:{'time':'value','time1':'value2'}其中time/time1和value/value2是startTimeInMillis值數組中的 和 值。json 解組時我做錯了什么?應該如何解組上述數據?
查看完整描述

2 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

您的App結構甚至與您嘗試解組的 json 文檔沒有密切相關。要解組 json 文檔,您必須有一個與底層文檔的結構有些匹配的 Go 結構。


type ResponseValue struct {

    StartTime int64 `json:"startTimeMillis"`

    // other elements of Values here, if you're interested in them

}


type Response struct {

  Id int `json:"Id"`

  Name string `json:"Name"`

  Path string `json:"Path"`

  Frequency string `json:"frequency"`

  Values []ResponseValue `json:"Values"`

}


type Body struct {

  Response []Response `json:"response"`

}


var data Body

json.Unmarshal([]byte(ame.Response),&data)

然后,您可以從 中提取時間序列data。


查看完整回答
反對 回復 2023-07-31
?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

這是你的響應結構

type Response struct {

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

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

? ? Path? ? ? string `json:"Path"`

? ? Frequency string `json:"frequency"`

? ? Values? ? []struct {

? ? ? ? StartTimeInMillis int64 `json:"startTimeInMillis"`

? ? ? ? Occurrences? ? ? ?int? ?`json:"occurrences"`

? ? ? ? Current? ? ? ? ? ?int? ?`json:"current"`

? ? ? ? Min? ? ? ? ? ? ? ?int? ?`json:"min"`

? ? ? ? Max? ? ? ? ? ? ? ?int? ?`json:"max"`

? ? ? ? UseRange? ? ? ? ? bool? `json:"useRange"`

? ? ? ? Count? ? ? ? ? ? ?int? ?`json:"count"`

? ? ? ? Sum? ? ? ? ? ? ? ?int? ?`json:"sum"`

? ? ? ? Value? ? ? ? ? ? ?int? ?`json:"value"`

? ? ? ? StandardDeviation int? ?`json:"standardDeviation"`

? ? } `json:"Values"`

}


查看完整回答
反對 回復 2023-07-31
  • 2 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號