如何将嵌套的JSON数据扁平化

6

尝试将嵌套的json响应从2层深度压平为1层。

这是我在Go Playground上的工作代码:http://play.golang.org/p/kHAYuZUTko

我想最终得到:

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??"`
}

...而不是包含嵌套的Facebook类型的Social类型(如下面的代码所示)。

我猜我需要编写一个自定义的UnmarshalJSON函数,但我不知道它们是如何工作的,而且我对Go语言还很陌生。

有什么建议吗?


这是来自上面Go Playground链接的代码:

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)
}
2个回答

10
如果您只是使用地图,这个功能将会很有用。
// 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
}

7

2
尽管这样做可以使用 social.FacebookLikes 访问它,但最终结果仍然嵌套了 Facebook。有没有办法使最终结果变得扁平化?也许根本不使用 Facebook 结构体? - Joe Bergevin
不,你想这么做有什么原因吗? - OneOfOne
2
你说得对。最终结果实际上是一样的。抱歉,我才学Go几天,还是个新手 :) - Joe Bergevin
1
我已经寻找这个东西很长时间了。谢谢你! - Jordan Stewart
这对于反序列化很有效,但对于序列化来说,我希望能够进行扁平化处理。 - hookenz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接