Go json tag 使用

Go Json Tag

1、struct接收json中的字符串

1、常规类型

当接收或者转换的类型为string类型时,可以在标签后面增加string标志

package main

import (
	"encoding/json"
	"fmt"
	"github.com/gogf/gf/os/gtime"
)

type Student struct {
	Id int64 `json:"id,string"`
	Name string `json:"name"`
	Age int64 `json:"age,string"`
	Ctime *gtime.Time `json:"ctime,string"`
}

func main()  {
	s := `{"id":"232","name":"咚咚咚","age":"17","ctime":"2021-02-18 09:30:34"}`
	s2 := new(Student)
	err := json.Unmarshal([]byte(s), s2)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(*s2)
}

/*
{232 咚咚咚 17 2021-02-18 09:30:34}
*/