前言
我们经常遇到如何将 map[string]interface{} 转化为 struct, 这个过程会用到反射, 通过反射可以实现,不确定的成员依然适用 map[string]interface{} 表示,确定结构后,再将 map[string]interface{} 解析为具体的某个结构。
主要使用的是 mapstructure 来实现,将 map 转换称 struct
一个第三方库,地址:https://github/mitchellh/mapstructure
json 转换成 map ,然后 map 转换成 struct
json 转换成 struct
json 转换成 struct 只需要使用 json.unmashal 即可
type User struct {
Name string
FansCount int64
}
func TestJsonUnmarshal(t *testing.T) {
const jsonStream = `
{"name":"ethancai", "fansCount": 9223372036854775807}
`
var user User /

发布评论