type a_1_struct_name struct {
Field1, Field2 string `json:"field_1"`
}
func main() {
a := a_1_struct_name{}
j := `{"field_1": "field_1_val"}`
if err := json.Unmarshal([]byte(j), &a); err != nil {
log.Fatal(err)
}
fmt.Println(a)
}
This code throws an error:
./prog.go:10:10: struct field Field2 repeats json tag "field_1" also at prog.go:10
see https://play.golang.org/p/sFZ-6GWUO0J
Why not use this:
type a_1_struct_name struct {
Field1 string `json:"field_1"`
Field2 string `json:"field_2"`
}
________________________________________
Von: golan...@googlegroups.com <golan...@googlegroups.com> im Auftrag von amarjeeta...@gmail.com <amarjeeta...@gmail.com>
Gesendet: Freitag, 3. Juli 2020 16:11
An: golang-nuts
Betreff: [go-nuts] ignore json tag for field list in a struct field
If there is a json tag with multiple fields in a single line, is there any json parser which will consider the json field name?
Like in below, "encoding/json" ignores the fields to unmarshal if there are multiple fields in a single line with a json tag.
EX: below code will print nothing.
type a_1_struct_name struct {
Field1, Field2 string `json:"field_1"`
}
func main() {
a := a_1_struct_name{}
j := `{"field_1": "field_1_val"}`
if err := json.Unmarshal([]byte(j), &a); err != nil {
log.Fatal(err)
}
fmt.Println(a)
}
But if we remove the json tag `json:"field_1"` and the change the value of json(j) accordingly, it prints {field_1_val }
I am writing a program to get the json equivalent tag of struct fields using reflect.
My question is, is it safe to ignore the fieldList(comma separated struct fields in a single line) if there is a json tag with it?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com<mailto:golang-nuts+unsubscribe@googlegroups.com>.