英文:
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的方式不正确。
你可以通过两种方式进行修复:
-
https://play.golang.org/p/LFO4tOLyG60
将结构扁平化
-
https://play.golang.org/p/TyFfaMf7XeF
在声明值时重复结构定义
第一种方式应该更容易和更清晰。
英文:
So you're initializing the jsonValue badly.
You can fix it in 2 ways:
-
https://play.golang.org/p/LFO4tOLyG60
making structures flat
-
https://play.golang.org/p/TyFfaMf7XeF
by repeating the structure definition when declaring value
The first one should be easier and clearer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论