json - 在go中动态编码json键

标签 json go marshalling

我希望 json 输出如下所示。请注意,基于参数类型,如 documenttext , 下一个键变为 documenttext .

  "components": [
        {
          "type" : "header",
          "parameters": [
          {
            "type": "document",
            "document": {
            "filename":"dummy.pdf",
              "link": "https://en.unesco.org/inclusivepolicylab/sites/default/files/dummy-pdf_2.pdf"
            }
          }
        ]
        },
        {
          "type" : "body",
          "parameters": [
            {
              "type": "text",
              "text": "replacement_text"
            }
          ] 
         }
         ]

这是我的结构定义:
type Component struct {
    Type       string      `json:"type,omitempty"`
    Parameters []Parameter `json:"parameters,omitempty"`
}

type Parameter struct {
    Type            string `json:"type,omitempty"`
    TypeInformation map[string]interface{}
}

当我对其进行编码时,它是这样的:
"components": [
        {
          "type": "body",
          "parameters": [
            {
              "type": "text",
              "TypeInformation": {
                "text": "Param1"
              }
            },
            {
              "type": "text",
              "TypeInformation": {
                "text": "param2"
              }
            }
          ]
        },
        {
          "type": "header",
          "parameters": [
            {
              "type": "document",
              "TypeInformation": {
                "document": {
                  "link": "http://link",
                  "filename": "dummy.pdf"
                }
              }
            }
          ]
        }
      ]

我不想要 TypeInformation关键是在 json 中,我只想要内部对象。我怎样才能做到这一点?

最佳答案

而不是像使用 Parameter 那样使用带有任意映射的“通用”结构您可以为每种参数类型使用不同的具体类型。然后,只需将它们放入一片空接口(interface)中,json.Marshal 就会知道该做什么。

type Object struct {
    Components []Component `json:"components"`
}

type Component struct {
    Type       string        `json:"type,omitempty"`
    Parameters []interface{} `json:"parameters,omitempty"`
}

type TextParameter struct {
    Type textType `json:"type"`
    Text string   `json:"text"`
}

type DocumentParameter struct {
    Type     documentType `json:"type"`
    Document Document     `json:"document"`
}

type Document struct {
    FileName string `json:"filename"`
    Link     string `json:"link"`
}

// used to "hard code" the type of the parameter
type textType struct{}

func (textType) MarshalJSON() ([]byte, error) { return []byte(`"text"`), nil }

// used to "hard code" the type of the parameter
type documentType struct{}

func (documentType) MarshalJSON() ([]byte, error) { return []byte(`"document"`), nil }

然后你可以像这样初始化一个实例:
obj := Object{
    Components: []Component{{
        Type: "header",
        Parameters: []interface{}{
            DocumentParameter{Document: Document{
                FileName: "dummy.pdf",
                Link:     "https://en.unesco.org/inclusivepolicylab/sites/default/files/dummy-pdf_2.pdf",
            }},
        },
    }, {
        Type: "body",
        Parameters: []interface{}{
            TextParameter{Text: "replacement_text"},
        },
    }},
}

https://play.golang.org/p/aNpnSGn980a

关于json - 在go中动态编码json键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61930633/

相关文章:

javascript - 来自两个 Controller 的 Angular 连接数据

javascript - 在自定义类上使用 JSON.stringify

javascript - 使用firebase存储数据以生成xml

unit-testing - 如何对pubsub进行单元测试接收回调

error-handling - 如何检测 gorm 中的连接失败?

c# - 从 C# 调用 C DLL 导致尝试读取或写入 protected 内存

通过 JAXB 响应的 Spring xml

json - Flutter:如何从 firebase 数据库中删除最后一个节点?

go - Go中同主机UDP包关联

java - 使用自定义类作为 JAX-WS 返回类型?