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

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

將 JSON 數組返回到 Struct Golang

將 JSON 數組返回到 Struct Golang

Go
慕的地6264312 2023-04-24 16:48:59
我的主要目標是將 JSON 對象傳遞回客戶端。但是,我的結構中不斷出現 nil 或空值。如何獲得預期和所需的 JSON 數組響應?下面是我的代碼片段。package mainimport (    "net/http"    "fmt"    "encoding/json")type News struct {    NewsID      int     `json:"newsId"`    PlayerID    int     `json:"playerId"`    TeamID      int     `json:"teamId"`    Team        string  `json:"team"`    Title       string  `json:"title"`    Content     string  `json:"content"`    Url         string  `json:"url"`    Source      string  `json:"source"`    TermsOfUse  string  `json:"terms"`    Updated     string  `json:"updated"`}func GetBoxScore (w http.ResponseWriter, r *http.Request) {    news := News{}    req, _ := http.NewRequest("GET","https://api.fantasydata.net/v3/nhlpb/scores/JSON/News", nil)    req.Header.Set("Ocp-Apim-Subscription-Key", "API KEY")    req.Host = "api.fantasydata.net"    client := &http.Client{}    res, err := client.Do(req)    defer res.Body.Close()    if err != nil {        fmt.Printf("The HTTP request failed with error %s\n", err)    }    err = json.NewDecoder(r.Body).Decode(&news)    newsJson, err := json.Marshal(news)    if err != nil {        panic(err)    }    w.Header().Set("Content-Type", "application/json")    w.WriteHeader(http.StatusAccepted)    w.Write(newsJson)}目前,響應是我的空 News 結構,所有值為 nil。我想要和期待的回應如下:  [        {            "NewsID": 8919,            "PlayerID": 30003647,            "TeamID": 28,            "Team": "VAN",            "Title": "Rumors have Elias Pettersson back this week",            "Content": "The rumor mill has Elias Pettersson (concussion) returning this week.",            "Url": "http://www.rotoworld.com/player/nhl/5819/elias-pettersson",            "Source": "NBCSports.com",            "TermsOfUse": "NBCSports.com feeds in the RSS format are provided free of charge for use by individuals for personal, non-commercial uses. More details here: http://fantasydata.com/resources/rotoworld-rss-feed.aspx",            "Updated": "2018-10-21T11:54:00"        },
查看完整描述

2 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

我要在這里提到兩件事。首先,你是否得到了你期望的回應?你可能想檢查一下。


第二,您提供的 json 是一組新聞,而不是單個新聞。您可能希望將新聞類型更改為數組而不是單個新聞。


type NewsItem struct {

    NewsID      int     `json:"newsId"`

    PlayerID    int     `json:"playerId"`

    TeamID      int     `json:"teamId"`

    Team        string  `json:"team"`

    Title       string  `json:"title"`

    Content     string  `json:"content"`

    Url         string  `json:"url"`

    Source      string  `json:"source"`

    TermsOfUse  string  `json:"terms"`

    Updated     string  `json:"updated"`

}


type News []NewsItem


查看完整回答
反對 回復 2023-04-24
?
胡子哥哥

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

在下一行

err = json.NewDecoder(r.Body).Decode(&news)

您正在傳遞新聞結構,因為 json 實際上是一個數組。因此,您需要創建一片新聞結構,然后傳遞它。

newsList := make([]News,0)
err = json.NewDecoder(r.Body).Decode(&newsList)


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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