我想返回一个如下所示的结构:
{
results: [
["ooid1", 2.0, "Söme text"],
["ooid2", 1.3, "Åther text"],
]
}
Run Code Online (Sandbox Code Playgroud)
这是一个arrags数组,是字符串,浮点数,unicode字符.
如果是Python我将能够:
import json
json.dumps({'results': [["ooid1", 2.0, u"Söme text"], ...])
Run Code Online (Sandbox Code Playgroud)
但是在Go中你不能拥有混合类型的数组(或切片).
我想过使用这样的结构:
type Row struct {
Ooid string
Score float64
Text rune
}
Run Code Online (Sandbox Code Playgroud)
但我不希望每个人都成为一本字典,我希望它成为一个包含3个元素的数组.
我们可以通过实现json.Marshaler接口来自定义对象的序列化方式.对于我们的特定情况,我们似乎有一些Row
元素,我们想要编码为异构值的数组.我们可以通过MarshalJSON
在我们的Row
类型上定义一个函数来实现,使用一个中间切片interface{}
来编码混合值.
此示例演示:
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))
}
Run Code Online (Sandbox Code Playgroud)
当然,我们也可能想要反过来,从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
}
Run Code Online (Sandbox Code Playgroud)
这使用了类似的技巧,首先使用中间切片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)
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里阅读完整的例子.
使用 []interface{}
type Results struct {
Rows []interface{} `json:"results"`
}
Run Code Online (Sandbox Code Playgroud)
如果要访问存储的值,则必须使用类型断言 []interface{}
for _, row := range results.Rows {
switch r := row.(type) {
case string:
fmt.Println("string", r)
case float64:
fmt.Println("float64", r)
case int64:
fmt.Println("int64", r)
default:
fmt.Println("not found")
}
}
Run Code Online (Sandbox Code Playgroud)