json marshal结构数组的一个属性 [英] json marshal one property of struct array
问题描述
代码: https://play.golang.org/p/E71d4xzNM4 结果: 通缉结果: 我怎样才能得到这个没有循环遍历整个数组,并创建一个新的数组结构,并对其做一个json元帅 <您可以定义自定义编组器: https://play.golang.org/p/xLUAlMllGR 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: Wanted Result: 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 You may define custom marshaller: https://play.golang.org/p/xLUAlMllGR 这篇关于json marshal结构数组的一个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
[
{
Id:1,
标题:精美的花生酱,
描述:世界上最好的花生酱,
来源:{
Name:Peter,
Address:32121 Little Midge
},
价格:49
},
{
Id:2,
标题:美好的果冻,
描述:世界上最好的果冻,
来源:{
Name:Peter,
Address:32121 Little Midge
},
Price:39
}
]
[
{
Name:Peter,
Address:32121 Little Midge
},
{
Name:Peter,
Address:32121 Little Midge
}
]
func(r Recipe)MarshalJSON()([] byte,error){
return json.Marshal(r.Source)
}
[
{
"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
}
]
[
{
"Name": "Peter",
"Address": "32121 Little Midge"
},
{
"Name": "Peter",
"Address": "32121 Little Midge"
}
]
func (r Recipe) MarshalJSON() ([]byte, error) {
return json.Marshal(r.Source)
}