![1693928149021.jpg](https://static.golangjob.cn/230905/3d9959561a33450d3495a2dc23331745.jpg)
我要怎么给引入的三方库结构体添加json格式化时候的小写字段呢,除了自己定义之外。谢谢各位。
三方库解君愁
```go
package main
import (
"fmt"
"strings"
jsoniter "github.com/json-iterator/go"
"github.com/json-iterator/go/extra"
)
func main() {
err := test()
if err != nil {
panic(err)
}
}
func test() error {
type Person struct {
Name string
Age int
}
type Student struct {
Person
School string `json:"school"`
Grade string `json:"grade"`
}
extra.SetNamingStrategy(strings.ToLower) // 设置名称转换规则
json := jsoniter.ConfigCompatibleWithStandardLibrary
s1 := new(Student)
s1.Name = "张三"
s1.Age = 20
s1.School = "家里蹲大学"
s1.Grade = "本科"
b, err := json.Marshal(s1)
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
```
结果:{"name":"张三","age":20,"school":"家里蹲大学","grade":"本科"}
#5
更多评论
type Person1 struct {
Name string `json:"name"`
Age int `json:"age"`
}
#1