从前端我得到了这个 json 示例:
{
"properties":{"unknown key": "unknown value","unknown key2": "unknown value 2"}
}
我开始用map[string]interface{}解析它,但它不起作用。我也不知道这个领域我能得到多少。可以是 10 或 1。
代码:
type test struct {
p map[string]string `json:"properties"`
}
func main() {
var t test
body := `
{
"properties":{"unknown key": "unknown value","unknown key2": "unknown value 2"}
}
`
json.Unmarshal([]byte(body), &t)
fmt.Println(t.p)
}
此代码始终返回空地图。
正确答案
您应该导出应解组的结构字段,例如:
type test struct {
P map[string]string `json:"properties"`
}
参见https://www.php.cn/link/eaf76caaba574ebf8e825f321c14ba29