1 回答

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊
你想要的輸出格式:
[["1455523840380",1],["1455523840383",2],["1455523840384",3]]
在 JSON 中,它不是結(jié)構(gòu)數(shù)組,而是數(shù)組數(shù)組。
由于“內(nèi)部”數(shù)組有多種類型(字符串和數(shù)字),您可以這樣建模:
type ChartElement []interface{}
你可以像這樣填充它:
s := []ChartElement{{"1455523840380", 1}, {"1455523840383", 2}, {"1455523840384", 3}}
如果您將其編組為 JSON:
data, err := json.Marshal(s)
fmt.Println(string(data), err)
輸出是你所期望的:
[["1455523840380",1],["1455523840383",2],["1455523840384",3]] <nil>
時(shí)間值例如1455523840380是自 1970 年 1 月 1 日 UTC 以來(lái)經(jīng)過(guò)的毫秒數(shù)。在 Go 中,您可以time.Time使用其Time.UnixNano()方法從值中獲取此值并將其除以1000000(從納秒中獲取毫秒),例如:
fmt.Println(time.Now().UnixNano() / 1000000) // Output: 1455526958178
請(qǐng)注意,為了在 JSON 輸出中將時(shí)間值作為字符串,您必須將這些時(shí)間值作為strings添加到[]ChartElement. 要將此毫秒值轉(zhuǎn)換為string,您可以使用strconv.FormatInt(),例如
t := time.Now().UnixNano() / 1000000
timestr := strconv.FormatInt(t, 10) // timestr is of type string
- 1 回答
- 0 關(guān)注
- 162 瀏覽
添加回答
舉報(bào)