I have a json which is as follows:
{
"parameters": {
"key1": {
"value": "someStringValue"
},
"key2": {
"innerKey1": {
"innerKey2": {
"innerKey3": "someStringValue"
},
}
}
"key4": {
"value": 123
},
"key5": {
"value": true
},
}
}
As you can see I can have different kind of values (string, map, int, bool). Following is the struct I've defined:
type TestStruct struct {
Parameters map[string]ParameterValue
}
type ParameterValue struct {
Values map[string]interface{}
}
With this I'm facing issues when parsing the json, key1,key2,key3
are not being marshalled and their value is nil
. What should be the correct struct design for this?
This seems to work fine:
package main
import (
"encoding/json"
"fmt"
)
var s = `
{
"parameters": {
"key1": {"value": "someStringValue"},
"key2": {
"innerKey1": {
"innerKey2": {"innerKey3": "someStringValue"}
}
},
"key4": {"value": 123},
"key5": {"value": true}
}
}
`
func main() {
var m struct {
Parameters map[string]interface{}
}
json.Unmarshal([]byte(s), &m)
fmt.Println(m.Parameters["key1"]) // map[value:someStringValue]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.