down00112 2016-07-25 23:00
浏览 173
已采纳

解析嵌套的JSON到结构?

I want to parse the following JSON output:

{
    "total":5689,
    "result":{
        "6581":{
            "percent":37.79,
            "count":2150
        },
        "6591":{
            "percent":35.31,
            "count":2009
        },
        "6601":{
            "percent":26.89,
            "count":1530
        }
    }
}

I have read that JSON can be parsed into a struct if the format is known:

package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type VoteResult struct {
    Total  int `json:"total"`
    Result struct {
        Efid1 struct {
            Percent float64 `json:"percent"`
            Count   int     `json:"count"`
        }
        Efid2 struct {
            Percent float64 `json:"percent"`
            Count   int     `json:"count"`
        }
        Efid3 struct {
            Percent float64 `json:"percent"`
            Count   int     `json:"count"`
        }
    }
}

func main() {
    b := []byte(`{"total":5689,"result":{"6581":{"percent":37.79,"count":2150}
    ,"6591":{"percent":35.31,"count":2009},"6601":{"percent":26.89,"count":1530}}}`)

    var v VoteResult

    err := json.Unmarshal(b, &v)
    if err != nil {
        fmt.Fprintf(os.Stderr, "%v
", err)
        os.Exit(1)
    }

    fmt.Println(v)
}

Go Playground

This is the output, but something is wrong as the nested structs are not filled with data:

{5689 {{0 0} {0 0} {0 0}}}

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dto52236 2016-07-25 23:05
    关注

    The result part of the JSON is a dictionary mapping strings to objects. Try this instead (https://play.golang.org/p/BCNHw-OH2I):

    type VoteResult struct {
        Total  int `json:"total"`
        Result map[string]struct {
            Percent float64 `json:"percent"`
            Count   int     `json:"count"`
        }
    }
    

    EDIT

    As an alternative, if those strings are truly fixed, you could do this:

    type VoteResult struct {
        Total  int `json:"total"`
        Result struct {
            Efid1 struct {
                Percent float64 `json:"percent"`
                Count   int     `json:"count"`
            } `json:"6581"`
            Efid2 struct {
                Percent float64 `json:"percent"`
                Count   int     `json:"count"`
            } `json:"6591"`
            Efid3 struct {
                Percent float64 `json:"percent"`
                Count   int     `json:"count"`
            } `json:"6601"`
        }
    }
    

    Here we've just decided that Efid1 has the JSON key 6591, etc. But I suspect a map of strings to structs is a better fit for the data structure you have.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接