如何在golang中序列化一个字典

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

How to serialize a dictionary in golang

问题

我尝试复制这个请求体以便在请求中使用:

  1. {"Responses":[{"type":"DROP_DOWN","value":"0"}]}

所以我正在做的是:

  1. type FruitBasket struct {
  2. Name5 []string `json:"Responses"`
  3. }
  4. form := payload{
  5. Name5: []string{"type", "value"},
  6. }
  7. jsonData, err := json.Marshal(form)
  8. fmt.Println(string(jsonData))

但是我找不到一种方法来完成括号内的内容。

英文:

I try to replicate this body form in order to use it in a request:

  1. {"Responses":[{"type":"DROP_DOWN","value":"0"}]}

so what im doing is :

  1. type FruitBasket struct {
  2. Name5 []string `json:"Responses"`
  3. }
  4. form := payload{
  5. Name5: []string{"type", "value"},
  6. }
  7. jsonData, err := json.Marshal(form)
  8. fmt.Println(string(jsonData))

But i can't find a way to complete the body in the brackets

答案1

得分: 2

你需要使用"encoding/json"包中的Unmarshal函数,并使用一个虚拟结构体来提取切片字段。

  1. // 你可以编辑这段代码!
  2. // 点击这里开始输入。
  3. package main
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. )
  8. func main() {
  9. str := `{"Responses":[{"type":"DROP_DOWN","value":"0"}]}`
  10. type Responses struct {
  11. Type string `json:"type"`
  12. Value string `json:"value"`
  13. }
  14. // 添加虚拟结构体来保存响应
  15. type Dummy struct {
  16. Responses []Responses `json:"Responses"`
  17. }
  18. var res Dummy
  19. err := json.Unmarshal([]byte(str), &res)
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Println("%v", len(res.Responses))
  24. fmt.Println("%s", res.Responses[0].Type)
  25. fmt.Println("%s", res.Responses[0].Value)
  26. }
英文:

You need to use the Unmarshal function from "encoding/json" package and use a dummy struct to extract the slice fields

  1. // You can edit this code!
  2. // Click here and start typing.
  3. package main
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. )
  8. func main() {
  9. str := `{"Responses":[{"type":"DROP_DOWN","value":"0"}]}`
  10. type Responses struct {
  11. Type string `json:"type"`
  12. Value string `json:"value"`
  13. }
  14. // add dummy struct to hold responses
  15. type Dummy struct {
  16. Responses []Responses `json:"Responses"`
  17. }
  18. var res Dummy
  19. err := json.Unmarshal([]byte(str), &res)
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Println("%v", len(res.Responses))
  24. fmt.Println("%s", res.Responses[0].Type)
  25. fmt.Println("%s", res.Responses[0].Value)
  26. }

答案2

得分: 0

JSON-to-go 是一个很好的在线资源,可以根据特定的 JSON 模式生成 Go 日期类型。

将你的 JSON 主体粘贴进去并提取出嵌套类型,你可以使用以下类型来生成所需的 JSON 模式:

  1. // 用于生成 JSON 的类型:
  2. //
  3. // {"Responses":[{"type":"DROP_DOWN","value":"0"}]}
  4. type FruitBasket struct {
  5. Response []Attr `json:"Responses"`
  6. }
  7. type Attr struct {
  8. Type string `json:"type"`
  9. Value string `json:"value"`
  10. }

使用方法:

  1. form := FruitBasket{
  2. Response: []Attr{
  3. {
  4. Type: "DROP_DOWN",
  5. Value: "0",
  6. },
  7. },
  8. }
  9. jsonData, err := json.Marshal(form)

工作示例:https://go.dev/play/p/SSWqnyVtVhF

输出:

  1. {"Responses":[{"type":"DROP_DOWN","value":"0"}]}
英文:

JSON-to-go is a good online resource to craft Go date types for a particular JSON schema.

Pasting your JSON body & extracting out the nested types you could use the following types to generate the desired JSON schema:

  1. // types to produce JSON:
  2. //
  3. // {"Responses":[{"type":"DROP_DOWN","value":"0"}]}
  4. type FruitBasket struct {
  5. Response []Attr `json:"Responses"`
  6. }
  7. type Attr struct {
  8. Type string `json:"type"`
  9. Value string `json:"value"`
  10. }

to use:

  1. form := FruitBasket{
  2. Response: []Attr{
  3. {
  4. Type: "DROP_DOWN",
  5. Value: "0",
  6. },
  7. }
  8. }
  9. jsonData, err := json.Marshal(form)

working example: https://go.dev/play/p/SSWqnyVtVhF

output:

  1. {"Responses":[{"type":"DROP_DOWN","value":"0"}]}

答案3

得分: 0

你的结构体不正确。你的标题要求是字典,但你写了一个字符串的数组或切片。

将你的 FruitBasket 结构体从这个:

  1. type FruitBasket struct {
  2. Name5 []string `json:"Responses"`
  3. }

改为这个:

  1. type FruitBasket struct {
  2. Name5 []map[string]interface{} `json:"Responses"`
  3. }

map[string]interface{} 在 Go 中表示字典。

这是 playground 的链接:https://go.dev/play/p/xRSDGdZYfRN

英文:

Your struct is not correct. Your title want dictionary, but you write an array or slice of string.

Change your FruitBasket struct from this:

  1. type FruitBasket struct {
  2. Name5 []string `json:"Responses"`
  3. }

to this

  1. type FruitBasket struct {
  2. Name5 []map[string]interface{} `json:"Responses"`
  3. }

map[string]interface{} is dictionary in go

here's the playground https://go.dev/play/p/xRSDGdZYfRN

huangapple
  • 本文由 发表于 2022年3月1日 07:41:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/71302243.html
匿名

发表评论

匿名网友

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

确定