golang 多级json转map
func main() { jsonStr := `{"isSchemaConforming":true,"schemaVersion":0,"unknown.0":[{"email_address":"test1@uber.com"},{"email_address":"test2@uber.com"}]}` dynamic := make(map[string]interface{}) json.Unmarshal([]byte(jsonStr), &dynamic) firstEmail := dynamic["unknown.0"].([]interface{})[0].(map[string]interface{})["email_address"] fmt.Println(firstEmail) }
type Email struct { Email string `json:"email_address"` } type EmailsList struct { IsSchemaConforming bool `json:"isSchemaConforming"` SchemaVersion int `json:"schemaVersion"` Emails []Email `json:"unknown.0"` } func main() { jsonStr := `{"isSchemaConforming":true,"schemaVersion":0,"unknown.0":[{"email_address":"test1@uber.com"},{"email_address":"test2@uber.com"}]}` emails := EmailsList{} json.Unmarshal([]byte(jsonStr), &emails) fmt.Printf("%+v\n", emails) }
posted on 2019-09-04 19:14 ExplorerMan 阅读(3295) 评论(0) 编辑 收藏 举报