It can gets different json construction:
type 1:
{"errcode": 0, "errmsg": "OK", "data": {"name": "Jolly", "age": 19}}
type 2:
{"errcode": 0, "errmsg": "OK", "data": {"friend": "Lisa", "country": "US"}}
I knew I can get those by define 4 structs like:
type Base1 struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
Data Person `json:"data"`
}
type Person struct {
Age int `json:"age"`
Name string `json:"name"`
}
and
type Base2 struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
Data Info `json:"data"`
}
type Info struct {
Friend string `json:"friend"`
Country string `json:"country"`
}
If I want to load json 1, just type data := Base1{}
If I want to load json 2, just type data := Base2{}
But I want to know how can I get them by 3 structs and using interface, because I have lots of APIs that provide json construction, their first layer all same as struct Base
like
type Base struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
Data interface `json:"data"`
}
How to write the data := Base{...}
and let it become struct Base1, Base2