我们可以通过实现json.Marshaler 接口来自定义对象的序列化方式。对于我们的特殊情况,我们似乎有一个 Row 元素切片,我们希望将其编码为一组异构值。我们可以通过在 Row 类型上定义一个 MarshalJSON 函数来做到这一点,使用 interface{} 的中间切片来编码混合值。
This example 演示:
package main
import (
"encoding/json"
"fmt"
)
type Row struct {
Ooid string
Score float64
Text string
}
func (r *Row) MarshalJSON() ([]byte, error) {
arr := []interface{}{r.Ooid, r.Score, r.Text}
return json.Marshal(arr)
}
func main() {
rows := []Row{
{"ooid1", 2.0, "Söme text"},
{"ooid2", 1.3, "Åther text"},
}
marshalled, _ := json.Marshal(rows)
fmt.Println(string(marshalled))
}
当然,我们也可能想反过来,从 JSON 字节返回结构。所以我们可以使用类似的json.Unmarshaler 接口。
func (r *Row) UnmarshalJSON(bs []byte) error {
arr := []interface{}{}
json.Unmarshal(bs, &arr)
// TODO: add error handling here.
r.Ooid = arr[0].(string)
r.Score = arr[1].(float64)
r.Text = arr[2].(string)
return nil
}
这使用了类似的技巧,首先使用interface{} 的中间切片,使用解组器将值放入这个通用容器中,然后将值放回我们的结构中。
package main
import (
"encoding/json"
"fmt"
)
type Row struct {
Ooid string
Score float64
Text string
}
func (r *Row) UnmarshalJSON(bs []byte) error {
arr := []interface{}{}
json.Unmarshal(bs, &arr)
// TODO: add error handling here.
r.Ooid = arr[0].(string)
r.Score = arr[1].(float64)
r.Text = arr[2].(string)
return nil
}
func main() {
rows := []Row{}
text := `
[
["ooid4", 3.1415, "pi"],
["ooid5", 2.7182, "euler"]
]
`
json.Unmarshal([]byte(text), &rows)
fmt.Println(rows)
}
您可以阅读完整示例here。