在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

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

Try this:

package main

import (
    "encoding/json"
    "fmt"
)


type dataPredictions struct {
    SinglePredictions []SinglePredictions `json:"predictions"`
}

type SinglePredictions struct {
    Predictions []float64 `json:"predictions"`
}

func main() {
    s := []byte(`{"predictions": [{"predictions": [4492.0]}, {"predictions": [4515.84716796875]}, {"predictions": [4464.86083984375]}]}`)

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