如何展平嵌套JSON [英] How to flatten nested JSON
问题描述
尝试将嵌套的json响应从2个级别展平到1个级别.
这是我在Go Playground上的工作代码: http://play.golang.org/p/kHAYuZUTko
我想结束:
type社会结构{GooglePlusPlusOnes uint32`Social:"GooglePlusOne"`TwitterTweets uint32`json:"Twitter"`LinkedinShares uint32`json:"LinkedIn"`PinterestPins uint32`json:"Pinterest"`uint32`json:"StumbleUpon"`DeliciousBookmarks uint32`json:"Delicious"`Facebook喜欢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 ??"}
...而不是包含嵌套的 Facebook
类型的 Social
类型(在下面的代码中可见).
我怀疑我需要做一个自定义的 UnmarshalJSON
函数,但是我不知道它们是如何工作的,而且我是Go的新手.
建议?
这是上面转到操场链接中的代码:
程序包主要进口 (编码/json""fmt")输入社交结构{GooglePlusPlusOnes uint32`json:"GooglePlusOne"`TwitterTweets uint32`json:"Twitter"`LinkedinShares uint32`json:"LinkedIn"`PinterestPins uint32`json:"Pinterest"`uint32`json:"StumbleUpon"`DeliciousBookmarks uint32`json:"Delicious"`Facebook的}输入Facebook struct {Facebook喜欢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 []社交err:= json.Unmarshal(jsonBlob,& social)如果err!= nil {fmt.Println(错误:",错误)}fmt.Printf(%+ v",社交)}
您可以简单地嵌入 Facebook结构:
type社会结构{.....Facebook`json:"Facebook"`}
然后按以下方式访问它:
social.FacebookLikes
工作示例: http://play.golang.org/p/xThhX_92Sg >
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)
}
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
这篇关于如何展平嵌套JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!