1 回答

TA貢獻2041條經驗 獲得超4個贊
第一步將 JSON 轉換為 go 結構。
type AutoGenerated struct {
? ? TotalGrand? ? ? int `json:"total_grand"`
? ? TotalBillable? ?int `json:"total_billable"`
? ? TotalCurrencies []struct {
? ? ? ? Currency string `json:"currency"`
? ? ? ? Amount? ?int? ? `json:"amount"`
? ? } `json:"total_currencies"`
? ? Data []struct {
? ? ? ? ID? ? int `json:"id"`
? ? ? ? Title struct {
? ? ? ? ? ? Project string? ? ? `json:"project"`
? ? ? ? ? ? Client? interface{} `json:"client"`
? ? ? ? } `json:"title"`
? ? ? ? Time? ? ? ? ? ? int `json:"time"`
? ? ? ? TotalCurrencies []struct {
? ? ? ? ? ? Currency string `json:"currency"`
? ? ? ? ? ? Amount? ?int? ? `json:"amount"`
? ? ? ? } `json:"total_currencies"`
? ? ? ? Items []struct {
? ? ? ? ? ? Title struct {
? ? ? ? ? ? ? ? TimeEntry string `json:"time_entry"`
? ? ? ? ? ? } `json:"title"`
? ? ? ? ? ? Time int? ? `json:"time"`
? ? ? ? ? ? Cur? string `json:"cur"`
? ? ? ? ? ? Sum? int? ? `json:"sum"`
? ? ? ? ? ? Rate int? ? `json:"rate"`
? ? ? ? } `json:"items"`
? ? } `json:"data"`
}
然后
將 json 文件讀入字節數組,即使用
http.NewRequest("GET", url, nil)
,client.Do(req)
和byte_array,err:=ioutil.ReadAll(resp.Body)
使用
make
或new
創建結構體的實例將字節數組處理為結構體實例
json.Unmarshal(byte_array, &instance_of_struct)
然后您將擁有一個包含 JSON 數據的結構
- 1 回答
- 0 關注
- 154 瀏覽
添加回答
舉報