golang默认将一个json对象解组到map [string] interface {}中,如何将其解组到[] byte?

golang unmarshal json object into map[string]interface{} by default, how can I unmarshal it into []byte? Because I need secondary unmarshal it into a struct instance after I got its Type.

Why don't you unmarshal the json into the struct directly?

Or in case you have more objects into slice of struct?

package main

import (
    "encoding/json"
    "fmt"
)

type TestJson struct {
    Foo string
    Baz string
}

var (
    jsonValue = `{"FOO" : "BAR", "BAZ" : "QUX"}`
    jsonValueSlice = `[{"FOO" : "BAR", "BAZ" : "QUX"},{"FOO" : "Second BAR", "BAZ" : "Second QUX"}]`
)

func main() {
    t := TestJson{}
    err := json.Unmarshal([]byte(jsonValue), &t)
    if err != nil {
        fmt.Println(err)
    }

    fmt.Printf("%+v
", t)


    t2 := []TestJson{}
    err2 := json.Unmarshal([]byte(jsonValueSlice), &t2)
    if err2 != nil {
        fmt.Println(err2)
    }

    fmt.Printf("%+v
", t2)
}

EDIT : Go doesn't unmarshal into an map[string]interface{} by default, read the docs!

func Unmarshal(data []byte, v interface{}) error

Golang json.Unmarshal can populate basically anyting, as long your data is consistant.