dpg98445 2017-10-18 13:22
浏览 106
已采纳

用数组中包含的[int]字符串映射解组JSON

Im trying to unmarshall the following JSON into a struct, but there I am unable to translate the contents of the values field with the [[int,string]] This is what I have so far:

type Response struct {
            Metric struct {
                Name string `json:"name,omitempty"`
                Appname string `json:"appname,omitempty"`
            } `json:"metric,omitempty"`
            Values []map[int]string `json:"values,omitempty"`
}

The JSON file:

{
   "metric":{
      "name":"x444",
      "appname":"cc-14-471s6"
   },
   "values":[
      [
         1508315264,
         "0.0012116165566900816"
      ],
      [
         1508315274,
         "0.0011871631158857396"
      ]
   ]
}
  • 写回答

1条回答 默认 最新

  • duanqi5114 2017-10-18 13:26
    关注

    The data you showed should be unmarshaled to:

    type Response struct {
                Metric struct {
                    Name string `json:"name,omitempty"`
                    Appname string `json:"appname,omitempty"`
                } `json:"metric,omitempty"`
                Values [][]interface{} `json:"values,omitempty"`
    }
    

    If you want to to transfer it to map implement json.Unmarshaller interface - https://golang.org/pkg/encoding/json/#Unmarshaler

    You can have something like:

    type Item struct {
        Key int
        Val string
    }
    func(item *Item) UnmarshalJSON([]byte) error {
        // TODO: implement 
    }
    
    type Response struct {
                Metric struct {
                    Name string `json:"name,omitempty"`
                    Appname string `json:"appname,omitempty"`
                } `json:"metric,omitempty"`
                Values []Item  `json:"values,omitempty"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测