...
garray
模块下的所有容器类型均实现了标准库json
数据格式的序列化/反序列化接口。 1.
Marshal
...
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/garray"
)
func main() {
type Student struct {
Id int
Name string
Scores *garray.IntArray
}
s := Student{
Id: 1,
Name: "john",
Scores: garray.NewIntArrayFrom([]int{100, 99, 98}),
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
}
```
执行后,输出结果:
...
```
...
{"Id":1,"Name":"john","Scores":[100,99,98]}
...
```
...
Unmarshal
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/garray"
)
func main() {
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
type Student struct {
Id int
Name string
Scores *garray.IntArray
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
}
执行后,输出结果:
{1 john [100,99,98]}