Is it possible to have a struct with multiple JSON tags?

后端 未结 2 1249
情深已故
情深已故 2021-01-19 04:16

I post a request to a server and get a reply in JSON format. I\'m able to unmarshal it to a struct. Then I need to create a new JSON file with the same data but different JS

2条回答
  •  一整个雨季
    2021-01-19 04:45

    It's not possible. The encoding/json package only handles the json key in struct tags. If the json key is listed multiple times (as in your example), the first occurrence will be used (this is implemented in StructTag.Get()).

    Note that this is an implementation restriction of the encoding/json package and not that of Go. One could easily create a similar JSON encoding package supporting either multiple tag keys (e.g. json1, json2) or multiple occurrences of the same key (as in your example).

提交回复
热议问题