英文:
Go : format struct for javascript (json without keys)
问题
我必须为图表形成一个结构体切片。将其编组,并返回给前端小部件。
小部件正在等待以下格式的数据:
[["1455523840380",1],["1455523840383",2],["1455523840384",3]]
但是我的数据是这样的:
[{"Time":1.45552462158e+12,"Value":1},{"Time":1.45552462158e+12,"Value2},{"Time":1.45552462158e+12,"Value3}]
我的结构体如下所示:
type ChartElement struct {
Time int `json:""`
Value int `json:""`
}
我现在有两个主要问题:
- 如何生成没有键,只有逗号分隔的两个值的 JSON?
- 如何正确将日期或时间转换为 JavaScript 毫秒?
英文:
I have to form a slice of structs for the chart. Marshal it, and return to the frontend widget.
Widget is waiting for the format like this :
[["1455523840380",1],["1455523840383",2],["1455523840384",3]]
But My data comes like this :
[{"Time":1.45552462158e+12,"Value":1},{"Time":1.45552462158e+12,"Value2},{"Time":1.45552462158e+12,"Value3}]
My struct that is coming to the slice is made like this :
type ChartElement struct {
Time int `json:""`
Value int `json:""`
}
I have now 2 main troubles:
- how to make the json without keys, but just 2 values with comma
between them? - how to convert date or time to the javascript
miliseconds correctly?
答案1
得分: 2
你想要的输出格式是:
[["1455523840380",1],["1455523840383",2],["1455523840384",3]]
在JSON中,它不是一个结构体数组,而是一个数组的数组。
由于"internal"数组具有不同的类型(字符串和数字),你可以这样建模:
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>
时间值(如1455523840380
)表示自1970年1月1日UTC以来经过的毫秒数。在Go中,你可以使用time.Time
值的Time.UnixNano()
方法获取此值,并将其除以1000000
(从纳秒转换为毫秒),例如:
fmt.Println(time.Now().UnixNano() / 1000000) // 输出:1455526958178
请注意,为了在JSON输出中将时间值作为字符串,你必须将这些时间值作为[]ChartElement
中的string
添加。要将此毫秒值转换为string
,你可以使用strconv.FormatInt()
,例如:
t := time.Now().UnixNano() / 1000000
timestr := strconv.FormatInt(t, 10) // timestr 的类型为 string
英文:
The output format you want:
[["1455523840380",1],["1455523840383",2],["1455523840384",3]]
In JSON it is not an array of struct, but an array of arrays.
Since the "internal" array has various types (string and numeric), you can model it like this:
type ChartElement []interface{}
And you can populate it like this:
s := []ChartElement{{"1455523840380", 1}, {"1455523840383", 2}, {"1455523840384", 3}}
And if you marshal it to JSON:
data, err := json.Marshal(s)
fmt.Println(string(data), err)
Output is what you expect:
[["1455523840380",1],["1455523840383",2],["1455523840384",3]] <nil>
And the time values such as 1455523840380
are the the number of milliseconds elapsed since January 1, 1970 UTC. In Go you can get this value from a time.Time
value with its Time.UnixNano()
method and dividing it by 1000000
(to get milliseconds from nanoseconds), for example:
fmt.Println(time.Now().UnixNano() / 1000000) // Output: 1455526958178
Note that in order to have time values as strings in the JSON output, you have to add these time values as string
s in the []ChartElement
. To convert this millisecond value to string
, you can use strconv.FormatInt()
, e.g.
t := time.Now().UnixNano() / 1000000
timestr := strconv.FormatInt(t, 10) // timestr is of type string
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论