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

    评论

报告相同问题?

悬赏问题

  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题