从结构而不是结构值创建JSON [英] Creating JSON from struct, not struct value
本文介绍了从结构而不是结构值创建JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想我需要反射包来做到这一点.说我有一个像这样的结构:
I imagine I would need the reflect package to do this. Say I have a struct like so:
type Inner struct {
Whatever int
}
type ResolutionValue struct {
Val string
Inner
}
说我想生成ResolutionValue结构的JSON表示...是否有某种方法可以创建不实例化的JSON表示呢?
say I want to generate a JSON representation of the ResolutionValue struct...is there some way to create a JSON representation without instantiating it?
推荐答案
即使使用反射,您也需要使用实际值才能使用reflect.TypeOf
获取类型.因此,我建议您只使用类型的零值.
Even with reflection, you would need an actual value to get the type using reflect.TypeOf
. So I would suggest just using the zero value of your type.
json.Marshal(ResolutionValue{})
这篇关于从结构而不是结构值创建JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文