问题
Is it possible to unmarshal JSON into a struct made from reflection without hardcoding the original type?
package main
import (
"fmt"
"encoding/json"
"reflect"
)
type Employee struct {
Firstname string `json:"firstname"`
}
func main() {
//Original struct
orig := new(Employee)
t := reflect.TypeOf(orig)
v := reflect.New(t.Elem())
//Reflected struct
new := v.Elem().Interface().(Employee)
// Unmarshal to reflected struct
json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), &new)
fmt.Printf("%+v\n", new)
}
I used a cast to Employee
in this example. But what if i don't know the type?
When i just use v
for the unmarhaling the struct will be zeroed.
json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), v)
When I omit the cast I get a map. which is understandable
json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), v.Elem().Interface())
回答1:
The problem here is that if you omit the type assertion here:
new := v.Elem().Interface()
The new
is inferred to have a interface{}
type.
Then when you take the address to unmarshal, the type of &new
is *interface{}
(pointer to interface{}) and unmarshal does not work as you expect.
You can avoid the type assertion if instead of getting the Elem()
you work directly with the pointer reference.
func main() {
//Original struct
orig := new(Employee)
t := reflect.TypeOf(orig)
v := reflect.New(t.Elem())
// reflected pointer
newP := v.Interface()
// Unmarshal to reflected struct pointer
json.Unmarshal([]byte("{\"firstname\": \"bender\"}"), newP)
fmt.Printf("%+v\n", newP)
}
Playground: https://play.golang.org/p/lTBU-1PqM4
回答2:
If you don't know the type at all, you can Unmarshal the JSON string into an interface{}. If you then need to work with the Unmarshaled data, you can convert it to the desired type.
Here's an example:
package main
import (
"encoding/json"
"fmt"
"reflect"
"unsafe"
)
type Employee struct {
Firstname string `json:"firstName"`
}
func deserialize(jsonData string) interface{} {
var obj interface{}
if err := json.Unmarshal([]byte(jsonData), &obj); err != nil {
panic(err)
}
return obj
}
func NewEmployee(objData map[string]interface{}) *Employee {
s := (*Employee)(nil)
t := reflect.TypeOf(s).Elem()
employeePtr := reflect.New(t)
employee := (*Employee)(unsafe.Pointer(employeePtr.Pointer()))
employee.Firstname = objData["firstName"].(string)
return employee
}
func main() {
jsonData := "{\"firstName\": \"John\"}"
obj := deserialize(jsonData)
objData := obj.(map[string]interface{})
employee := NewEmployee(objData)
fmt.Printf("%s\n", employee.Firstname)
}
You can check it on the Go Playground.
来源:https://stackoverflow.com/questions/45679408/unmarshal-json-to-reflected-struct