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

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

How to serialize a dictionary in golang

问题

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

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

所以我正在做的是:

type FruitBasket struct {
    Name5 []string `json:"Responses"`
}
form := payload{
    Name5: []string{"type", "value"},
}
jsonData, err := json.Marshal(form)
fmt.Println(string(jsonData))

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

英文:

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

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

so what im doing is :

type FruitBasket struct {
		Name5 []string `json:"Responses"`
		
	}
form := payload{
		Name5: []string{"type", "value"},
	}
	jsonData, err := json.Marshal(form)
	fmt.Println(string(jsonData))

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

答案1

得分: 2

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

// 你可以编辑这段代码!
// 点击这里开始输入。
package main

import (
	"encoding/json"
	"fmt"
)

func main() {

	str := `{"Responses":[{"type":"DROP_DOWN","value":"0"}]}`

	type Responses struct {
		Type  string `json:"type"`
		Value string `json:"value"`
	}

	// 添加虚拟结构体来保存响应
	type Dummy struct {
		Responses []Responses `json:"Responses"`
	}
	var res Dummy
	err := json.Unmarshal([]byte(str), &res)
	if err != nil {
		panic(err)
	}
	fmt.Println("%v", len(res.Responses))
	fmt.Println("%s", res.Responses[0].Type)
	fmt.Println("%s", res.Responses[0].Value)

}
英文:

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

// You can edit this code!
// Click here and start typing.
package main

import (
	"encoding/json"
	"fmt"
)

func main() {

	str := `{"Responses":[{"type":"DROP_DOWN","value":"0"}]}`

	type Responses struct {
		Type  string `json:"type"`
		Value string `json:"value"`
	}

	// add dummy struct to hold responses
   type Dummy struct {
		Responses []Responses `json:"Responses"`
	}
	var res Dummy
	err := json.Unmarshal([]byte(str), &res)
	if err != nil {
		panic(err)
	}
	fmt.Println("%v", len(res.Responses))
	fmt.Println("%s", res.Responses[0].Type)
	fmt.Println("%s", res.Responses[0].Value)

}

答案2

得分: 0

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

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

// 用于生成 JSON 的类型:
//
//    {"Responses":[{"type":"DROP_DOWN","value":"0"}]}

type FruitBasket struct {
	Response []Attr `json:"Responses"`
}

type Attr struct {
	Type  string `json:"type"`
	Value string `json:"value"`
}

使用方法:

form := FruitBasket{
	Response: []Attr{
		{
			Type:  "DROP_DOWN",
			Value: "0",
		},
	},
}

jsonData, err := json.Marshal(form)

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

输出:

{"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:

// types to produce JSON:
//
//    {"Responses":[{"type":"DROP_DOWN","value":"0"}]}

type FruitBasket struct {
	Response []Attr `json:"Responses"`
}

type Attr struct {
	Type  string `json:"type"`
	Value string `json:"value"`
}

to use:

form := FruitBasket{
	Response: []Attr{
		{
			Type:  "DROP_DOWN",
			Value: "0",
		},
    }
}

jsonData, err := json.Marshal(form)

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

output:

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

答案3

得分: 0

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

将你的 FruitBasket 结构体从这个:

type FruitBasket struct {
	Name5 []string `json:"Responses"`
}

改为这个:

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

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:

type FruitBasket struct {
	Name5 []string `json:"Responses"`
}

to this

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

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:

确定