doukong5394 2014-07-08 22:35
浏览 181
已采纳

Go / Golang,拼合嵌套的JSON

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)
}
  • 写回答

2条回答 默认 最新

  • dongzhang4301 2014-07-08 22:48
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题