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

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

與Golang中的結(jié)構(gòu)體數(shù)組混淆

與Golang中的結(jié)構(gòu)體數(shù)組混淆

Go
慕碼人8056858 2021-04-02 13:15:00
我正在嘗試與JSON API進(jìn)行交互。它有兩個(gè)端點(diǎn):GetTravelTimeAsJSON-指定一個(gè)旅行時(shí)間ID,它返回一個(gè)旅行時(shí)間GetTravelTimesAsJSON-返回一個(gè)包含以上所有TravelTimes的數(shù)組。所以我有一個(gè)像這樣的結(jié)構(gòu):type TravelTime struct {  AverageTime int     `json:"AverageTime"`  CurrentTime int     `json:"CurrentTime"`  Description string  `json:"Description"`  Distance    float64 `json:"Distance"`  EndPoint    struct {    Description string  `json:"Description"`    Direction   string  `json:"Direction"`    Latitude    float64 `json:"Latitude"`    Longitude   float64 `json:"Longitude"`    MilePost    float64 `json:"MilePost"`    RoadName    string  `json:"RoadName"`  } `json:"EndPoint"`  Name       string `json:"Name"`  StartPoint struct {    Description string  `json:"Description"`    Direction   string  `json:"Direction"`    Latitude    float64 `json:"Latitude"`    Longitude   float64 `json:"Longitude"`    MilePost    float64 `json:"MilePost"`    RoadName    string  `json:"RoadName"`  } `json:"StartPoint"`  TimeUpdated  string `json:"TimeUpdated"`  TravelTimeID int    `json:"TravelTimeID"`}如果在一次旅行中這樣調(diào)用API,我將得到一個(gè)填充的結(jié)構(gòu)(我正在使用此req lib)header := req.Header{    "Accept":          "application/json",    "Accept-Encoding": "gzip",  }  r, _ := req.Get("http://www.wsdot.com/Traffic/api/TravelTimes/TravelTimesREST.svc/GetTravelTimeAsJson?AccessCode=<redacted>&TravelTimeID=403", header)  var foo TravelTime  r.ToJSON(&foo)  dump.Dump(foo)如果我轉(zhuǎn)儲(chǔ)響應(yīng),它看起來像這樣:TravelTime {  AverageTime: 14 (int),  CurrentTime: 14 (int),  Description: "SB I-5 Pierce King County Line To SR 512",  Distance: 12.06 (float64),  EndPoint:  {    Description: "I-5 @ SR 512 in Lakewood",    Direction: "S",    Latitude: 47.16158351 (float64),    Longitude: -122.481133 (float64),    MilePost: 127.35 (float64),    RoadName: "I-5"  }, JSON在這里:https : //gist.github.com/jaxxstorm/0ab818b300f65cf3a46cc01dbc35bf60如果我將原始TravelTime結(jié)構(gòu)修改為像這樣的切片,它將起作用:type TravelTimes []struct {}但是它不能作為一個(gè)單獨(dú)的響應(yīng)。我以前已經(jīng)做到了,但是由于某種原因,這種失敗使我的大腦無法正常工作。任何幫助表示贊賞。
查看完整描述

2 回答

?
互換的青春

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

解組不起作用是因?yàn)樗谕攲邮蔷哂衒ield的對(duì)象TravelTime,而頂層實(shí)際上是對(duì)象數(shù)組。您只想直接解組成一片對(duì)象,就像這樣:

  var foo []TravelTime
  r.ToJSON(&foo)


查看完整回答
反對(duì) 回復(fù) 2021-04-19
  • 2 回答
  • 0 關(guān)注
  • 347 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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