I have a json unmarshal requirement to a struct containing an interface{}. The inner type is known only at runtime, hence the struct is defined with interface{}. However, the type is populated correctly before passing to json.Unmarshal. But the json.Unmarshal always populates the resulting struct with map[] instead of given type. How do I resolve this to get the correct unmarshal behavior here.
See the simple test code and behavior here :
package main
import (
"fmt"
// s "strings"
"encoding/json"
)
type one struct {
S string `json:"status"`
Res interface{} `json:"res"`
}
type two struct {
A string
B string
}
func main() {
t := []two {{A:"ab", B:"cd",}, {A:"ab1", B:"cd1",}}
o := one {S:"s", Res:t}
js, _ := json.Marshal(&o)
fmt.Printf("o: %+v\n", o)
fmt.Printf("js: %s\n", js)
er := json.Unmarshal(js, &o)
fmt.Printf("er: %+v\n", er)
fmt.Printf("o: %+v\n", o)
}
Result:
o: {S:s Res:[{A:ab B:cd} {A:ab1 B:cd1}]}
js: {"status":"s","res":[{"A":"ab","B":"cd"},{"A":"ab1","B":"cd1"}]}
er: <nil>
o: {S:s Res:[map[A:ab B:cd] map[B:cd1 A:ab1]]}
See Code here1
owhich already contains a value. - Himanshu