英文:
How to flatten nested JSON
问题
尝试将嵌套的 JSON 响应从两层深度展平为一层。您怀疑需要编写一个自定义的 UnmarshalJSON
函数,但是对于如何编写这样的函数以及对 Go 语言不熟悉。
建议如下:
您可以使用匿名结构体来展平嵌套的 JSON 响应。首先,将 Social
结构体中的 Facebook
字段更改为匿名结构体。然后,使用 json:"字段名"
标签将嵌套的字段映射到相应的字段名。这样,您就可以直接访问嵌套字段的值。
以下是修改后的代码示例:
package main
import (
"encoding/json"
"fmt"
)
type Social struct {
GooglePlusPlusOnes uint32 `json:"GooglePlusOne"`
TwitterTweets uint32 `json:"Twitter"`
LinkedinShares uint32 `json:"LinkedIn"`
PinterestPins uint32 `json:"Pinterest"`
StumbleuponStumbles uint32 `json:"StumbleUpon"`
DeliciousBookmarks uint32 `json:"Delicious"`
Facebook `json:"Facebook"`
}
type Facebook struct {
FacebookLikes uint32 `json:"like_count"`
FacebookShares uint32 `json:"share_count"`
FacebookComments uint32 `json:"comment_count"`
FacebookTotal uint32 `json:"total_count"`
}
func main() {
var jsonBlob = []byte(`[
{"StumbleUpon":0,"Reddit":0,"Facebook":{"commentsbox_count":4691,"click_count":0,"total_count":298686,"comment_count":38955,"like_count":82902,"share_count":176829},"Delicious":0,"GooglePlusOne":275234,"Buzz":0,"Twitter":7346788,"Diggs":0,"Pinterest":40982,"LinkedIn":0}
]`)
var social []Social
err := json.Unmarshal(jsonBlob, &social)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", social)
}
这样,您就可以直接访问 Social
结构体中的 Facebook
字段,而不需要通过嵌套的结构体来访问。
希望这可以帮助到您!
英文:
Trying to flatten a nested json response from 2 levels deep down to 1.
Here is my working code on Go Playground: http://play.golang.org/p/kHAYuZUTko
I want to end up with:
type Social struct {
GooglePlusPlusOnes uint32 `Social:"GooglePlusOne"`
TwitterTweets uint32 `json:"Twitter"`
LinkedinShares uint32 `json:"LinkedIn"`
PinterestPins uint32 `json:"Pinterest"`
StumbleuponStumbles uint32 `json:"StumbleUpon"`
DeliciousBookmarks uint32 `json:"Delicious"`
FacebookLikes uint32 `json:"??some_magical_nested_address??"`
FacebookShares uint32 `json:"??some_magical_nested_address??"`
FacebookComments uint32 `json:"??some_magical_nested_address??"`
FacebookTotal uint32 `json:"??some_magical_nested_address??"`
}
...instead of a Social
type containing a nested Facebook
type (seen in the code below).
I suspect I'll need to do a custom UnmarshalJSON
function, but I don't know how those work and I'm new to Go.
Suggestions?
Here is the code from the Go Playground link above:
package main
import (
"encoding/json"
"fmt"
)
type Social struct {
GooglePlusPlusOnes uint32 `json:"GooglePlusOne"`
TwitterTweets uint32 `json:"Twitter"`
LinkedinShares uint32 `json:"LinkedIn"`
PinterestPins uint32 `json:"Pinterest"`
StumbleuponStumbles uint32 `json:"StumbleUpon"`
DeliciousBookmarks uint32 `json:"Delicious"`
Facebook Facebook
}
type Facebook struct {
FacebookLikes uint32 `json:"like_count"`
FacebookShares uint32 `json:"share_count"`
FacebookComments uint32 `json:"comment_count"`
FacebookTotal uint32 `json:"total_count"`
}
func main() {
var jsonBlob = []byte(`[
{"StumbleUpon":0,"Reddit":0,"Facebook":{"commentsbox_count":4691,"click_count":0,"total_count":298686,"comment_count":38955,"like_count":82902,"share_count":176829},"Delicious":0,"GooglePlusOne":275234,"Buzz":0,"Twitter":7346788,"Diggs":0,"Pinterest":40982,"LinkedIn":0}
]`)
var social []Social
err := json.Unmarshal(jsonBlob, &social)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", social)
}
答案1
得分: 9
如果你只使用地图,这个函数将会很有用。
// Flatten函数接受一个地图,并返回一个新的地图,其中嵌套的地图被替换为点分隔的键。
func Flatten(m map[string]interface{}) map[string]interface{} {
o := make(map[string]interface{})
for k, v := range m {
switch child := v.(type) {
case map[string]interface{}:
nm := Flatten(child)
for nk, nv := range nm {
o[k+"."+nk] = nv
}
default:
o[k] = v
}
}
return o
}
英文:
If you are only using maps this function will be useful.
// Flatten takes a map and returns a new one where nested maps are replaced
// by dot-delimited keys.
func Flatten(m map[string]interface{}) map[string]interface{} {
o := make(map[string]interface{})
for k, v := range m {
switch child := v.(type) {
case map[string]interface{}:
nm := Flatten(child)
for nk, nv := range nm {
o[k+"."+nk] = nv
}
default:
o[k] = v
}
}
return o
}
答案2
得分: 7
你可以简单地将Facebook结构体嵌入进去:
type Social struct {
.....
Facebook `json:"Facebook"`
}
然后可以这样访问它:
social.FacebookLikes
工作示例:http://play.golang.org/p/xThhX_92Sg
英文:
You can simply embed the Facebook struct:
type Social struct {
.....
Facebook `json:"Facebook"`
}
Then access it like:
social.FacebookLikes
Working example: http://play.golang.org/p/xThhX_92Sg
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论