serializing a JSON array of mixed types

658 views
Skip to first unread message

pala.d...@gmail.com

unread,
Jun 18, 2014, 9:10:28 AM6/18/14
to golan...@googlegroups.com
Hello, is there some way in Go json package to produce a JSON array of mixed types, something like this:

[
    3,
    "19223201",
    {
        "status": "Accepted",
        "currentTime": "2013-02-01T20:53:32.486Z",
        "heartbeatInterval": 300
    }
]

Thanks a lot,
Daniele Pala

Elson Wu

unread,
Jun 18, 2014, 9:37:58 AM6/18/14
to golan...@googlegroups.com
You have to use interface{}

http://play.golang.org/p/dQRmq9-L23

package main
import (
"encoding/json"
"fmt"
"time"
)
func main() {
val := []interface{}{}
val = append(val, 3)
val = append(val, "123123")
val = append(val, struct {
Status            string
CurrentTime       time.Time
HeartbeatInterval int
}{
"Accepted",
time.Now(),
300,
})
js, _ := json.Marshal(val)
fmt.Printf("%#v", string(js))
}


在 2014年6月18日星期三UTC+8下午9时10分28秒,pala.d...@gmail.com写道:

pala.d...@gmail.com

unread,
Jun 18, 2014, 9:46:24 AM6/18/14
to golan...@googlegroups.com
thanks a lot!

Daniele Pala
Reply all
Reply to author
Forward
0 new messages