【问题标题】:How to flatten nested JSON如何展平嵌套的 JSON
【发布时间】:2014-08-29 19:45:40
【问题描述】:

尝试将嵌套的 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 link的代码:

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)
}

【问题讨论】:

    标签: json go unmarshalling


    【解决方案1】:

    如果您只使用地图,此功能将很有用。

    // 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】:

      您可以简单地embed Facebook 结构:

      type Social struct {
          .....
          Facebook            `json:"Facebook"`
      }
      

      然后像这样访问它:

      social.FacebookLikes
      

      工作示例:http://play.golang.org/p/xThhX_92Sg

      【讨论】:

      • 尽管这样我可以使用 social.FacebookLikes 访问它,但最终结果仍然嵌套了 Facebook。有没有办法使最终结果平坦?也许根本不使用 Facebook 结构?
      • 不,你有什么理由要这样做吗?
      • 你是对的。最终结果实际上是相同的。抱歉,前几天才开始学习 Go,所以还是个菜鸟 :)
      • 我找这个很久了。谢谢你!
      猜你喜欢
      • 2018-12-23
      • 2021-10-12
      • 2016-10-06
      • 2021-09-05
      • 1970-01-01
      • 2021-07-14
      • 2021-10-19
      • 2012-05-29
      • 2020-03-08
      相关资源
      最近更新 更多