encode/json

结构体转json

package main

import (
    "fmt"
    "encoding/json"
)

type Student struct {
    Name string
    Age int
    Gradle int
}

func main() {

    s := Student{"xiaoming",10,4}

    result, err := json.Marshal(s)

    fmt.Printf("%s\n",result)
}

json转结构体

stu := new(Student)

json.Unmarshal([]byte(result),stu)

fmt.Println(*stu)