英文:
How to get the json field names of a struct in golang?
问题
获取该结构体的JSON字段名称的方法是什么?
type example struct {
Id int `json:"id"`
CreatedAt string `json:"created_at"`
Tag string `json:"tag"`
Text string `json:"text"`
AuthorId int `json:"author_id"`
}
我尝试使用以下函数打印字段:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
fmt.Println(val.Type().Field(i).Tag.Get("json"))
}
}
当然,我得到的结果是:
id
created_at
tag
text
author_id
但我想要的结果是:
id
created_at
tag
text
author_id
英文:
What is the way to get the json field names of this struct ?
type example struct {
Id int `json:"id"`
CreatedAt string `json:"created_at"`
Tag string `json:"tag"`
Text string `json:"text"`
AuthorId int `json:"author_id"`
}
I try to print the fields with this function :
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
fmt.Println(val.Type().Field(i).Name)
}
}
Of course I get :
Id
CreatedAt
Tag
Text
AuthorId
But I would like something like :
id
created_at
tag
text
author_id
答案1
得分: 42
你可以使用StructTag类型来获取标签。我提供的链接中有示例,请查阅,但你的代码可能类似于:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
fmt.Println(val.Type().Field(i).Tag.Get("json"))
}
}
注意:json标签的格式支持更多内容,不仅仅是字段名,例如omitempty或string,所以如果你需要处理这些情况,可以进一步改进PrintFields函数:
- 我们需要检查
json标签是否为-(即json:"-")。 - 我们需要检查名称是否存在并将其分离出来。
可以像这样修改代码:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
t := val.Type().Field(i)
fieldName := t.Name
switch jsonTag := t.Tag.Get("json"); jsonTag {
case "-":
case "":
fmt.Println(fieldName)
default:
parts := strings.Split(jsonTag, ",")
name := parts[0]
if name == "" {
name = fieldName
}
fmt.Println(name)
}
}
}
英文:
You use the StructTag type to get the tags. The documentation I linked has examples, look them up, but your code could be something like
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
fmt.Println(val.Type().Field(i).Tag.Get("json"))
}
}
NOTE The json tag format supports more than just field names, such as omitempty or string, so if you need an approach that takes care of that too, further improvements to the PrintFields function should be made:
- we need to check whether the
jsontag is-(i.e.json:"-") - we need to check if name exists and isolate it
Something like this:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
t := val.Type().Field(i)
fieldName := t.Name
switch jsonTag := t.Tag.Get("json"); jsonTag {
case "-":
case "":
fmt.Println(fieldName)
default:
parts := strings.Split(jsonTag, ",")
name := parts[0]
if name == "" {
name = fieldName
}
fmt.Println(name)
}
}
}
答案2
得分: 5
使用StructField的Name而不是Tag,可以使用Tag来获取StructTag对象。
参见:https://golang.org/pkg/reflect/#StructTag
然后,您可以使用StructTag的Get或Lookup方法来获取json标签:
使用Get:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
// 如果字段没有json标签,则打印空行
fmt.Println(val.Type().Field(i).Tag.Get("json"))
}
}
使用Lookup:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
// 跳过没有json标签的字段
if tag, ok := val.Type().Field(i).Tag.Lookup("json"); ok {
fmt.Println(tag)
}
}
}
英文:
Instead of using StructField's Name, you can use Tag to get a StructTag object.
See: https://golang.org/pkg/reflect/#StructTag
Then you can use StructTag's Get or Lookup methods to get the json tag:
Using Get:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
// prints empty line if there is no json tag for the field
fmt.Println(val.Type().Field(i).Tag.Get("json"))
}
}
Using Lookup:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
// skips fields without json tag
if tag, ok := val.Type().Field(i).Tag.Lookup("json"); ok {
fmt.Println(tag)
}
}
}
答案3
得分: 3
使用:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
t := val.Type()
for i := 0; i < t.NumField(); i++ {
fmt.Println(t.Field(i).Tag.Get("json"))
}
}
在playground中查看。
英文:
Use:
func (b example) PrintFields() {
val := reflect.ValueOf(b)
t := val.Type()
for i := 0; i < t.NumField(); i++ {
fmt.Println(t.Field(i).Tag.Get("json"))
}
}
See it in playground.
答案4
得分: 3
一个带有通用接口作为参数的更新版本:
func PrintFields(b interface{}) {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
t := val.Type().Field(i)
fieldName := t.Name
if jsonTag := t.Tag.Get("json"); jsonTag != "" && jsonTag != "-" {
// 检查可能的逗号,例如 "...,omitempty"
var commaIdx int
if commaIdx = strings.Index(jsonTag, ","); commaIdx < 0 {
commaIdx = len(jsonTag)
}
fieldName = jsonTag[:commaIdx]
}
fmt.Println(fieldName)
}
}
英文:
an updated version with a generic interface as parameter:
func PrintFields(b interface{}) {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
t := val.Type().Field(i)
fieldName := t.Name
if jsonTag := t.Tag.Get("json"); jsonTag != "" && jsonTag != "-" {
// check for possible comma as in "...,omitempty"
var commaIdx int
if commaIdx = strings.Index(jsonTag, ","); commaIdx < 0 {
commaIdx = len(jsonTag)
}
fieldName = jsonTag[:commaIdx]
}
fmt.Println(fieldName)
}
}
答案5
得分: 2
func (e example) GetJsonField() []string {
b := example{}
marshaled, _ := json.Marshal(b)
m := make(map[string]interface{})
_ = json.Unmarshal(marshaled, &m)
result := make([]string, 0)
for k := range m {
result = append(result, k)
}
return result
}
英文:
func (e example) GetJsonField() []string {
b := example{}
marshaled, _ := json.Marshal(b)
m := make(map[string]interface{})
_ = json.Unmarshal(marshaled, &m)
result := make([]string, 0)
for k := range m {
result = append(result, k)
}
return result
}
答案6
得分: 1
你所寻找的不是Name,而是Tag。
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
fmt.Println(val.Type().Field(i).Tag.Get("json"))
}
}
(翻译结果已经返回,不包含其他内容)
英文:
Not the Name you are looking for. What you looking is the Tag
func (b example) PrintFields() {
val := reflect.ValueOf(b)
for i := 0; i < val.Type().NumField(); i++ {
fmt.Println(val.Type().Field(i).Tag.Get("json"))
}
}
答案7
得分: 0
获取结构字段名的Json字段名
val := reflect.ValueOf(structobject)
field, _ := val.Type().FieldByName("Id")
fmt.Println(field.Tag.Get("json"))
获取Json字段名的代码部分已经翻译完成。
英文:
Get Json field name Using Struct Field Name
val := reflect.ValueOf(structobject)
field, _ := val.Type().FieldByName("Id")
fmt.Println(field.Tag.Get("json"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论