将不带键的 JSON 字符串数组转换为 GoLang 结构体。

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

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"`
}

tocc这两个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

ccto都是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"`
}

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

答案2

得分: 1

使用以下方法将JSON转换为Golang中的结构体:

Json-goStruct

请注意处理可能是Golang结构体的映射(maps),以及反之。

英文:

Use the below to convert the json to struct in golang:

Json-goStruct

Take care of the maps, which might be go struct and vice-versa.

huangapple
  • 本文由 发表于 2017年4月30日 04:04:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/43700000.html
匿名

发表评论

匿名网友

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

确定