使用一个子结构的数组填充一个结构体,以便将其转换为 JSON。

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

Populate a struct with an array of sub-structs to turn into json

问题

我正在尝试发布一些 JSON 数据。使用 JSON-to-Go 工具,我定义了以下结构体:

type IssueSetState struct {
	ID           string `json:"id"`
	CustomFields []struct {
		Value struct {
			Name string `json:"name"`
		} `json:"value"`
		Name string `json:"name"`
		Type string `json:"$type"`
	} `json:"customFields"`
}

我试图用一些数据填充它,然后将其传递给 http 库:

jsonValues := &IssueSetState{
	ID: resultEntityId.ID,
	CustomFields: []struct {
		Value: struct {
			Name: "Fixed",
		},
		Name: "State",
		Type: "StateIssueCustomField",
	},
}
jsonEncoded := new(bytes.Buffer)
json.NewEncoder(jsonEncoded).Encode(jsonValues)

我一直收到以下错误:

./main.go:245:19: 语法错误: 意外的 {,期望类型
./main.go:246:9: 语法错误: 意外的 :,期望逗号或 }
./main.go:249:8: 语法错误: 语句结束后的意外 :
./main.go:251:4: 语法错误: 顶层声明后的意外逗号

我确定我犯了一个简单的错误,但我对 Go 还不熟悉。

英文:

I’m trying to post some JSON. Using the JSON-to-Go tool I have this struct defined:

type IssueSetState struct {
	ID           string `json:"id"`
	CustomFields []struct {
		Value struct {
			Name string `json:"name"`
		} `json:"value"`
		Name string `json:"name"`
		Type string `json:"$type"`
	} `json:"customFields"`
}

I’m trying to populate it with some data that I can then pass into the http library:

	jsonValues := &IssueSetState{
		ID: resultEntityId.ID,
		CustomFields: []{
			Value: {
				Name: "Fixed",
			},
			Name: "State",
			Type: "StateIssueCustomField",
		},
	}
	jsonEncoded := new(bytes.Buffer)
	json.NewEncoder(jsonEncoded).Encode(jsonValues)

I keep getting errors like:

./main.go:245:19: syntax error: unexpected {, expecting type
./main.go:246:9: syntax error: unexpected :, expecting comma or }
./main.go:249:8: syntax error: unexpected : at end of statement
./main.go:251:4: syntax error: unexpected comma after top level declaration

I’m sure the mistake I’m making is a simple one, but I’m new to Go.

答案1

得分: 1

一种可能的方法是为每个匿名结构定义命名结构体。

type IssueSetState struct {
	ID           string        `json:"id"`
	CustomFields []CustomField `json:"customFields"`
}

type CustomField struct {
	Value Value  `json:"value"`
	Name  string `json:"name"`
	Type  string `json:"type"`
}

type Value struct {
	Name string `json:"name"`
}

现在你可以像这样创建它:

IssueSetState{
	ID: resultEntityId.ID,
	CustomFields: []CustomField{
		{
			Value: Value{
				Name: "Fixed",
			},
			Name: "State",
			Type: "StateIssueCustomField",
		},
		{
			Value: Value{
				Name: "Fixed",
			},
			Name: "State",
			Type: "StateIssueCustomField",
		},
	},
}
英文:

One possible way is to define named structs for every anonymous struct you have.

type IssueSetState struct {
	ID           string        `json:"id"`
	CustomFields []CustomField `json:"customFields"`
}

type CustomField struct {
	Value Value  `json:"value"`
	Name  string `json:"name"`
	Type  string `json:"type"`
}

type Value struct {
	Name string `json:"name"`
}

Now you can create it like this:

IssueSetState{
	ID: resultEntityId.ID,
	CustomFields: []CustomField{
		{
			Value: Value{
				Name: "Fixed",
			},
			Name: "State",
			Type: "StateIssueCustomField",
		},
		{
			Value: Value{
				Name: "Fixed",
			},
			Name: "State",
			Type: "StateIssueCustomField",
		},
	},
}

答案2

得分: 0

所以你初始化jsonValue的方式不正确。

你可以通过两种方式进行修复:

  1. https://play.golang.org/p/LFO4tOLyG60

    将结构扁平化

  2. https://play.golang.org/p/TyFfaMf7XeF

    在声明值时重复结构定义

第一种方式应该更容易和更清晰。

英文:

So you're initializing the jsonValue badly.

You can fix it in 2 ways:

  1. https://play.golang.org/p/LFO4tOLyG60

    making structures flat

  2. https://play.golang.org/p/TyFfaMf7XeF

    by repeating the structure definition when declaring value

The first one should be easier and clearer.

huangapple
  • 本文由 发表于 2021年10月7日 19:02:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/69479978.html
匿名

发表评论

匿名网友

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

确定