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

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

試圖從 JSON 響應(yīng)中獲取“Total”的值

試圖從 JSON 響應(yīng)中獲取“Total”的值

Go
動(dòng)漫人物 2022-06-21 10:42:43
回復(fù):{“元”:{“查詢(xún)時(shí)間”:0.039130201,“分頁(yè)”:{“偏移”:1345,“限制”:5000,“總計(jì)”:1345},結(jié)構(gòu):類(lèi)型 InLicense 結(jié)構(gòu) {Total int16 json:"total,omitempty"}類(lèi)型 OutLicense 結(jié)構(gòu) {分頁(yè) []InLicense json:"pagination,omitempty"}類(lèi)型 MetaLicense 結(jié)構(gòu) {Meta []OutLicense json:"meta,omitempty"}函數(shù)內(nèi)部的代碼片段:req, err := http.NewRequest("GET", , nil)if err != nil {//處理錯(cuò)誤}client := &http.Client{}resp, err := client.Do(req)if err != nil {log.Println("Error: ", err)}defer resp.Body.Close()val := &MetaLicense{ }err = json.NewDecoder(resp.Body).Decode(&val)if err != nil {    log.Fatal(err)}for _, s := range val.Meta {    for _, a := range s.Pagination {        fmt.Println(a.Total)    }}}運(yùn)行此代碼后,出現(xiàn)以下錯(cuò)誤: json: cannot unmarshal object into Go struct field MetaLicense.meta of type []OutLicense[]OutLicense 需要哪種類(lèi)型才能正確解組?我無(wú)法以其他方式打印它,但它使用 {} 打印,并且 Strings.Trim 不起作用。
查看完整描述

2 回答

?
一只萌萌小番薯

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

您應(yīng)該只使用具有實(shí)際類(lèi)型的簡(jiǎn)單字段聲明,而不是[]如下所示的類(lèi)型:


type InLicense struct {

    Total int16 json:"total,omitempty"

}


type OutLicense struct {

     Pagination InLicense json:"pagination,omitempty"

}


type MetaLicense struct {

    Meta OutLicense json:"meta,omitempty"

}


查看完整回答
反對(duì) 回復(fù) 2022-06-21
?
九州編程

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

我稍微簡(jiǎn)化了解析,只使用了該json.Unmarshal()函數(shù)。


raw := "{\n  \"meta\": {\n    \"query_time\": 0.039130201,\n    \"pagination\": {\n      \"offset\": 1345,\n      \"limit\": 5000,\n      \"total\": 1345\n    }\n  }\n}"


parsed := &MetaLicense{}

err := json.Unmarshal([]byte(raw), parsed)

if err != nil {

    log.Fatal(err)

}


fmt.Println(parsed.Meta.Pagination.Total) // Prints: 1345

這是我使用的類(lèi)型


type InLicense struct {

    Total int16 `json:"total,omitempty"`

}


type OutLicense struct {

    Pagination InLicense `json:"pagination,omitempty"`

}


type MetaLicense struct {

    Meta OutLicense `json:"meta,omitempty"`

}

如所寫(xiě),您提供的 JSON 有一個(gè)額外的,內(nèi)容,這使您的 json 無(wú)法解析(假設(shè)您也添加了缺少}的 's.


您的 JSON 中沒(méi)有列表。列表用[]符號(hào)表示。為了讓您的類(lèi)型正常工作,您的 JSON 必須如下所示:


{

  "meta": [{

    "query_time": 0.039130201,

    "pagination": [{

      "offset": 1345,

      "limit": 5000,

      "total": 1345

    }]

  }]

}


查看完整回答
反對(duì) 回復(fù) 2022-06-21
  • 2 回答
  • 0 關(guān)注
  • 300 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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