英文:
JSON string array without Keys into a GoLang struct
问题
我正在尝试解析从POST
请求提交的JSON数据,并将其映射到一个用于发送电子邮件的Web服务中的结构体中。以下JSON数据被提交到请求的主体中。
{
"body": {
"template": "abctemplate.html",
"params": {
"name": "Chase",
"email": "1234@gmail.com"
}
},
"to": [
"abc@gmail.com",
"xyz@gmail.com"
],
"cc": [
"xxx@example.com",
"yyy@example.com"
],
"replyTo": {
"email": "aaa@gmail.com",
"name": "Jack"
},
"bcc": "ccc@gmail.com",
"subject": "Hello, world!"
}
这些数据被映射并读入以下结构体中:
type emailData struct {
Body struct {
Template string `json:"template"`
Params map[string]string `json:"params"`
} `json:"body"`
To map[string]string `json:"to"` // TODO 这是错误的
CC string `json:"cc"` // TODO 这是错误的
ReplyTo struct {
Email string `json:"email"`
Name string `json:"name"`
}
BCC string `json:"bcc"`
Subject string `json:"subject"`
}
to
和cc
这两个JSON字段都是未知长度的字符串数组,并且没有键。有没有办法将这些字符串数组映射到结构体字段中?我尝试了两种不同的方式,但没有成功。谢谢!
英文:
I am trying to parse JSON that is submitted from a POST
request into a struct in a web service to send email. The following JSON is submitted in the body of the request.
{
"body": {
"template": "abctemplate.html",
"params": {
"name": "Chase",
"email": "1234@gmail.com"
}
},
"to": [
"abc@gmail.com",
"xyz@gmail.com"
],
"cc": [
"xxx@example.com",
"yyy@example.com"
],
"replyTo": {
"email": "aaa@gmail.com",
"name": "Jack"
},
"bcc": "ccc@gmail.com",
"subject": "Hello, world!"
}
This is mapped and read into the following struct
type emailData struct {
Body struct {
Template string `json:"template"`
Params map[string]string `json:"params"`
} `json:"body"`
To map[string]string `json:"To"` // TODO This is wrong
CC string `json:"cc"` // TODO this is wrong
ReplyTo struct {
Email string `json:"email"`
Name string `json:"name"`
}
BCC string `json:"bcc"`
Subject string `json:"subject"`
}
Both the 'to' and 'cc' JSON fields are string arrays of unknown length and do not have keys. Is there a way to map the string arrays into the struct fields? I've tried the two different ways where there are // TODO
tags with no luck. Thanks!
答案1
得分: 1
cc
和to
都是JSON数组,你可以将它们解组为Go切片,而不必担心长度。
type emailData struct {
Body struct {
Template string `json:"template"`
Params map[string]string `json:"params"`
} `json:"body"`
To []string `json:"to"`
CC []string `json:"cc"`
ReplyTo struct {
Email string `json:"email"`
Name string `json:"name"`
}
BCC string `json:"bcc"`
Subject string `json:"subject"`
}
链接:https://play.golang.org/p/Pi_5aSs922
英文:
Both cc
and to
are json arrays which you can unmarshal into Go slices without worrying about the length.
type emailData struct {
Body struct {
Template string `json:"template"`
Params map[string]string `json:"params"`
} `json:"body"`
To []string `json:"to"`
CC []string `json:"cc"`
ReplyTo struct {
Email string `json:"email"`
Name string `json:"name"`
}
BCC string `json:"bcc"`
Subject string `json:"subject"`
}
答案2
得分: 1
使用以下方法将JSON转换为Golang中的结构体:
请注意处理可能是Golang结构体的映射(maps),以及反之。
英文:
Use the below to convert the json to struct in golang:
Take care of the maps, which might be go struct and vice-versa.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论