解析嵌套在URL编码POST表单中的JSON字符串

解析嵌套在URL编码POST表单中的JSON字符串,json,forms,go,Json,Forms,Go,我正在尝试解析邮件通知webhook的一部分。 这是一个带有x-www-form-urlencoded正文的POST请求。 这是身体的一部分: sender: some@email.com attachments: [{"url": "https://storage.eu.mailgun.net/v3/domains/beep.boop/messages/randomstring/attachments/0", "content-type": "application/pdf", "name":

我正在尝试解析邮件通知webhook的一部分。 这是一个带有
x-www-form-urlencoded
正文的POST请求。 这是身体的一部分:

sender: some@email.com
attachments: [{"url": "https://storage.eu.mailgun.net/v3/domains/beep.boop/messages/randomstring/attachments/0", "content-type": "application/pdf", "name": "example.pdf", "size": 345}]"]
attachments
值是一个
json
编码的数组

我想将这个字符串从JSON解码为
StoredAttachment
嵌套结构,因为我正在将响应解码为
x-www-form-urlencoded
,但我不知道怎么做。目标结构如下所示:

type NotifiedMessage结构{
发件人字符串`schema:“发件人”`
主题字符串`schema:“主题”`
附件[]StoredAttachment`schema:“附件”`
MessageUrl字符串`schema:“消息url”`
}
//StoredAttachment结构包含与存储消息关联的附件的信息。
类型StoredAttachment结构{
Size int`json:“大小”`
Url字符串`json:“Url”`
名称字符串`json:“名称”`
ContentType字符串`json:“内容类型”`
}

这是我目前为止的非工作代码:

您可以实现
textumarshaler
接口,
schema
包将使用该接口,而不是执行默认过程,这允许自定义解组


1。声明一个命名类型,并将其用作
附件
字段的类型<代码>[]存储数据附件未命名。例如:

type AttachmentList []StoredAttachment
为什么??因为方法只能在命名类型上声明

2.实现
textumarhsaler
接口并在那里进行json解压

func (ls *AttachmentList) UnmarshalText(text []byte) (err error) {
    return json.Unmarshal(text, (*[]StoredAttachment)(ls))
}
就这样


非常感谢。我尝试了
textumarshaler
接口,但不知道这个命名类型约束。