[英]Dynamically create struct of some type and unmarshal JSON into that instance
我在运行时创建一个struct实例。 该实例已成功创建,但我无法将JSON解组到其中。
type Test struct {
Name string `json:"name,omitempty"`
}
func create(a interface{}) {
aType := reflect.TypeOf(a).Elem()
elType := aType.Elem()
f := reflect.Indirect(reflect.New(elType))
b := []byte(`{"name": "go"}`)
err := json.Unmarshal(b, &f)
fmt.Println(err, f)
}
func main() {
l := []Test{}
create(&l)
}
reflect.Indirect()
返回一个reflect.Value
类型的值,你应该将指向Test
(其类型为*Test
)的指针传递给json.Unmarshal()
。
只需使用Value.Interface()
方法获取指向reflect.New()
返回的Test
结构的指针,如下所示:
f := reflect.New(elType).Interface()
b := []byte(`{"name": "go"}`)
err := json.Unmarshal(b, f)
有了这个工作,输出将是:
<nil> &{go}
注意f
将是interface{}
类型,保持*Test
指针值。 如果需要struct值,可以使用类型断言,如下所示:
t := *f.(*Test)
fmt.Println(t)
这打印:
{go}
试试Go Playground上的例子。
我认为您不需要使用反射,而是可以执行以下操作:
type Test struct {
Name string `json:"name,omitempty"`
}
func create(a interface{}) {
b := []byte(`[{"name": "go"}]`)
err := json.Unmarshal(b, &a)
fmt.Println(err, a)
}
func main() {
l := []Test{}
create(&l)
}
代码reflect.Indirect(reflect.New(elType))
返回一个reflect.Value
代替的具体值elType
。 当你解组它时,json包会查看没有字段Name
的reflect.Value
结构,所以它什么都不做。
相反,您应该使用reflect.Value.Interface()
将其转换为保持具体类型的interface{}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.