Go : format struct for javascript (json without keys)

huangapple go评论78阅读模式
英文:

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:""`
}

我现在有两个主要问题:

  1. 如何生成没有键,只有逗号分隔的两个值的 JSON?
  2. 如何正确将日期或时间转换为 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:

  1. how to make the json without keys, but just 2 values with comma
    between them?
  2. 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:

[[&quot;1455523840380&quot;,1],[&quot;1455523840383&quot;,2],[&quot;1455523840384&quot;,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{{&quot;1455523840380&quot;, 1}, {&quot;1455523840383&quot;, 2}, {&quot;1455523840384&quot;, 3}}

And if you marshal it to JSON:

data, err := json.Marshal(s)
fmt.Println(string(data), err)

Output is what you expect:

[[&quot;1455523840380&quot;,1],[&quot;1455523840383&quot;,2],[&quot;1455523840384&quot;,3]] &lt;nil&gt;

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 strings 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

huangapple
  • 本文由 发表于 2016年2月15日 16:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/35404588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定