Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

garray模块下的所有容器类型均实现了标准库json数据格式的序列化/反序列化接口。 1.

  1. Marshal

...

  1.  
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]}