dsifjgogw48491752 2018-06-20 18:47
浏览 514

在golang中将字符串(地图数组)转换为json对象

I am trying to convert this json-format string into an actual json object in GOLANG.

{"predictions": [{"predictions": [4492.0]}, {"predictions": [4515.84716796875]}, {"predictions": [4464.86083984375]}]}

Basically, it is a dictionary with a key "predictions" and the value es an array of dictionaries (each dictionary is has as a key "predictions" and a value an 1-element float array. I created two structure (one for the first dictionary and the other one for the array of dictionaries), but I can't fit the string json to my structure. I am not sure what I am missing

package main

import (
    "encoding/json"
    "fmt"
)


type dataPredictions struct {
    SinglePredictions *SinglePredictions `json:"predictions"`
}

type SinglePredictions struct {
    Predictions []map[string]int `json:predictions`
}

func main() {

    s := `{"predictions": [{"predictions": [4492.0]}, {"predictions": [4515.84716796875]}, {"predictions": [4464.86083984375]}]}`

    data := &dataPredictions{
        SinglePredictions: &SinglePredictions{},
    }
    err := json.Unmarshal([]byte(s), data)
  s2, _ := json.Marshal(data)
    fmt.Println(err)
    fmt.Println(data.SinglePredictions)
    fmt.Println(string(s2))

}

The error I get is below.

json: cannot unmarshal array into Go struct field dataPredictions.predictions of type main.SinglePredictions
  • 写回答

2条回答 默认 最新

  • drphfy1198 2018-06-20 19:02
    关注

    There are basically two mistakes. The first is that you didn't define SinglePredictions as a slice, which is why you got the error in the first place, and then you used the map when you simply needed to pass down []float64.

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type dataPredictions struct {
        SinglePredictions []*SinglePredictions `json:"predictions"`
    }
    
    type SinglePredictions struct {
        Predictions []float64 `json:"predictions"`
    }
    
    func main() {
        s := `{"predictions": [{"predictions": [4492.0]}, {"predictions": [4515.84716796875]}, {"predictions": [4464.86083984375]}]}`
        // Note that you don't need to assign values manually - json.Unmarshal
        // will do that for you.
        data := &dataPredictions{}
        err := json.Unmarshal([]byte(s), data)
        s2, _ := json.Marshal(data)
        fmt.Println(err)
        fmt.Println(data.SinglePredictions)
        fmt.Println(string(s2))
    
    }
    

    The mistake you made seems that to have been thinking that Go would have unmarshalled the first array into dataPredictions.SinglePredictions.Predictions. But in dataPredictions you have a field which selects the "predictions" key in the topmost object, and its value is then passed to unmarshal into a *SinglePredictions

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度