// Given the following struct:
type MyStruct struct {
First string
Second string
Third string
}
// I would like to unmarshal the following JSON into MyStruct such as:
bytes := []byte({ { "first": { "href":"http://some/resources/1" },
"second": { "href":"http://http://some/resources/2" },
"third": { "href":"http://some/resources/3" } })
var s MyStruct
err := json.Unmarshal(bytes, &s)
fmt.Println(s.First)
// how do I make this return "http://some/resources/1" instead of
// "map[href:http://some resources/1]"?
What I'm looking for is something like a combination of go field tags with actual JSON object notation where I would be able to declare MyStruct
like this:
type MyStruct struct {
First string `json:"first.href"`
Second string `json:"second.href"`
Third string `json:"third.href"`
}
Any ideas?
The json package doesn't support a way to access nested fields (unlike encoding/xml). So you will either have to write you own Unmarshal function (see: JSON decoding in go ) or encapsulate the fields with accessors.
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.