json - 如何展平嵌套的 JSON

标签 json go unmarshalling

尝试将嵌套的 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)
}

最佳答案

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

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

关于json - 如何展平嵌套的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24642575/

相关文章:

javascript - AngularJS - 访问服务对象的 JS 对象名称/值对时出现类型错误

c# - 反序列化为 double 时 JsonConvert 抛出 'not a valid integer' 异常

jquery - 从 JSON 对象获取单个值

json - 避免科学记数法

json - 如何解决接口(interface){}是字符串,而不是golang中的[]uint8?

ruby - 在Sinatra中,如何测试一个既能返回html又能返回json的路由?

go - kubernetes golang客户端:在容器中获取容器

go - Aerospike Go Client : No available connections to the node. 连接池为空,限制连接数

json - 当 json 输入结构错误时,Unmarshall 应返回错误

JSON 在 go 中将相同的结构编码/解码为不同的 JSON 格式?