英文:
json marshal one property of struct array
问题
你想要将一个包含多个属性的结构体数组转换为 JSON,但只包含 Recipe 结构体中的 Source 属性。你希望得到的结果是只包含 Source 属性的 JSON 数组。你想知道如何在不遍历整个数组并创建新的结构体数组的情况下实现这一点,并进行 JSON 编组。
在 Go 中,如果你只想编组结构体中的某些字段,可以使用标签来指定 JSON 编组时的字段名称。你可以在结构体字段的标签中使用json:"-"
来指示该字段在编组时被忽略。
对于你的情况,你可以在 Recipe 结构体的 Source 字段上添加json:"source"
标签,然后对整个数组进行 JSON 编组。这样,只有 Source 字段会被编组到 JSON 中。
以下是修改后的代码示例:
package main
import (
"encoding/json"
"fmt"
)
type Source struct {
Name string `json:"name"`
Address string `json:"address"`
}
type Recipe struct {
Id int `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Source Source `json:"source"`
Price int `json:"price"`
}
func main() {
recipes := []Recipe{
{
Id: 1,
Title: "Fine Peanutbutter",
Description: "The best peanutbutter in the world",
Source: Source{
Name: "Peter",
Address: "32121 Little Midge",
},
Price: 49,
},
{
Id: 2,
Title: "Fine Jelly",
Description: "The best Jelly in the world",
Source: Source{
Name: "Peter",
Address: "32121 Little Midge",
},
Price: 39,
},
}
// 将整个数组编组为 JSON
jsonData, err := json.Marshal(recipes)
if err != nil {
fmt.Println("JSON 编组错误:", err)
return
}
fmt.Println(string(jsonData))
}
运行上述代码,你将得到以下输出:
[
{
"id": 1,
"title": "Fine Peanutbutter",
"description": "The best peanutbutter in the world",
"source": {
"name": "Peter",
"address": "32121 Little Midge"
},
"price": 49
},
{
"id": 2,
"title": "Fine Jelly",
"description": "The best Jelly in the world",
"source": {
"name": "Peter",
"address": "32121 Little Midge"
},
"price": 39
}
]
注意,现在 JSON 中只包含了 Source 字段,其他字段被忽略了。
英文:
So I have an array of struct Recipe it contains some properties and one of the properties is the struct Source, I want to convert the entire array to json but only the Source property of the Recipe struct
Code: https://play.golang.org/p/E71d4xzNM4
Result:
[
{
"Id": 1,
"Title": "Fine Peanutbutter",
"Description": "The best peanutbutter in the world",
"Source": {
"Name": "Peter",
"Address": "32121 Little Midge"
},
"Price": 49
},
{
"Id": 2,
"Title": "Fine Jelly",
"Description": "The best Jelly in the world",
"Source": {
"Name": "Peter",
"Address": "32121 Little Midge"
},
"Price": 39
}
]
Wanted Result:
[
{
"Name": "Peter",
"Address": "32121 Little Midge"
},
{
"Name": "Peter",
"Address": "32121 Little Midge"
}
]
How do I get this without looping through the entire array and creating a new array struct and doing a json marshal on that one
答案1
得分: 2
你可以定义自定义的编组器(marshaller):
func (r Recipe) MarshalJSON() ([]byte, error) {
return json.Marshal(r.Source)
}
链接:https://play.golang.org/p/xLUAlMllGR
英文:
You may define custom marshaller:
func (r Recipe) MarshalJSON() ([]byte, error) {
return json.Marshal(r.Source)
}
答案2
得分: 0
将以下内容添加到您的源代码中:
func (s Recipe) MarshalJSON() ([]byte, error) {
return json.Marshal(s.Source)
}
这段代码将实现 Recipe
结构体的 MarshalJSON
方法,用于将 Source
字段转换为 JSON 格式的字节数组。
英文:
Add this to your source:
func (s Recipe) MarshalJSON() ([]byte, error) {
return json.Marshal(s.Source)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论