How to unmarshal escaped JSON string with UnmarshalJSON

huangapple go评论122阅读模式
英文:

How to unmarshal escaped JSON string with UnmarshalJSON

问题

我正在尝试解析以下JSON字符串:

  1. token = `{
  2. "id": 1,
  3. "token": {
  4. "id": 2248637,
  5. "metadata": {
  6. "name": "Name #1",
  7. "formats": "[{\"mimeType\": \"model/gltf-binary\", \"uri\": \"uri1\"}, {\"mimeType\": \"image/gif\", \"uri\": \"uri2\"}]"
  8. }
  9. }
  10. }`

我可以像这样使用两个阶段进行解析。然而,我想使用自定义的unmarshalJSON方法,但是失败了,出现了错误:

我的代码如下:

  1. type FileFormat struct {
  2. MIMEType string
  3. URI string
  4. }
  5. func (format *FileFormat) UnmarshalJSON(data []byte) error {
  6. var aux []interface{}
  7. if err := json.Unmarshal(data, &aux); err != nil {
  8. return err
  9. }
  10. format.MIMEType = aux[0].(string)
  11. format.URI = aux[1].(string)
  12. return nil
  13. }
  14. type TokenMetadata struct {
  15. Name string `json:"name"`
  16. Formats []FileFormat `json:"formats"`
  17. }
  18. type Token struct {
  19. ID TokenID `json:"tokenId"`
  20. Metadata TokenMetadata `json:"metadata"`
  21. }
  22. func main() {
  23. var tokenRes OwnedToken
  24. if err := json.Unmarshal([]byte(token), &tokenRes); err != nil {
  25. fmt.Println(err)
  26. }
  27. }

错误信息为:

json: cannot unmarshal string into Go struct field TokenMetadata.token.metadata.formats of type []main.FileFormat

你如何解决这个问题?非常感谢!

英文:

I'm trying to unmarshal the following JSON string

  1. token = `{
  2. "id": 1,
  3. "token": {
  4. "id": 2248637,
  5. "metadata": {
  6. "name": "Name #1",
  7. "formats": "[{\"mimeType\": \"model/gltf-binary\", \"uri\": \"uri1\", {\"mimeType\": \"image/gif\", \"uri\": \"uri2\"}]"
  8. }
  9. }`

I can unmarshal it with 2 phases like this. However, I would like to use custom unmarshalJSON but I fail. I got error

My code as follow:

  1. type FileFormat struct {
  2. MIMEType string
  3. URI string
  4. }
  5. func (format *FileFormat) UnmarshalJSON(data []byte) error {
  6. var aux []interface{}
  7. if err := json.Unmarshal(data, &aux); err != nil {
  8. return err
  9. }
  10. format.MIMEType = aux[0].(string)
  11. format.URI = aux[1].(string)
  12. return nil
  13. }
  14. type TokenMetadata struct {
  15. Name string `json:"name"`
  16. Formats []FileFormat `json:"formats"`
  17. }
  18. type Token struct {
  19. ID TokenID `json:"tokenId"`
  20. Metadata TokenMetadata `json:"metadata"`
  21. }
  22. func main() {
  23. var tokenRes OwnedToken
  24. if err := json.Unmarshal([]byte(token), &tokenRes); err != nil {
  25. fmt.Println(err)
  26. }
  27. }

And the error is

  1. json: cannot unmarshal string into Go struct field TokenMetadata.token.metadata.formats of type []main.FileFormat

How can I fix this problem? Many thanks!

答案1

得分: 1

以下是翻译好的内容:

文件格式的JSON数组是双重编码的。声明一个与该数组对应的Go类型。在该类型的UnmarshalJSON方法中进行双重解码。

  1. type FileFormats []FileFormat
  2. func (ff *FileFormats) UnmarshalJSON(data []byte) error {
  3. var s string
  4. if err := json.Unmarshal(data, &s); err != nil {
  5. return err
  6. }
  7. return json.Unmarshal(
  8. []byte(s),
  9. (*[]FileFormat)(ff))
  10. }
  11. type TokenMetadata struct {
  12. Name string `json:"name"`
  13. Formats FileFormats `json:"formats"`
  14. }

注意:从*FileFormats*[]FileFormat的转换是为了防止递归。

英文:

The JSON array of file formats is double encoded. Declare a Go type corresponding to the array. Double decode in the UnmarshalJSON method for that type.

  1. type FileFormats []FileFormat
  2. func (ff *FileFormats) UnmarshalJSON(data []byte) error {
  3. var s string
  4. if err := json.Unmarshal(data, &s); err != nil {
  5. return err
  6. }
  7. return json.Unmarshal(
  8. []byte(s),
  9. (*[]FileFormat)(ff))
  10. }
  11. type TokenMetadata struct {
  12. Name string `json:"name"`
  13. Formats FileFormats `json:"formats"`
  14. }

Note: The conversion from from *FileFormats to *[]FileFormat is required to prevent recursion.

huangapple
  • 本文由 发表于 2022年9月13日 05:50:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/73695593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定