【问题标题】:How can I convert a string (array of maps) into a struct in GOLANG?如何将字符串(映射数组)转换为 GOLANG 中的结构?
【发布时间】:2020-03-05 15:17:50
【问题描述】:

我正在尝试将此 json 格式的字符串转换为 GOLANG 中的实际 json 对象。

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

基本上,它是一个带有键“预测”的字典,值是一个字典数组(每个字典都有一个键“预测”和一个 1 元素浮点数组的值。我创建了两个结构(一个对于第一个字典和另一个字典数组),但我无法将字符串 json 适合我的结构。我不确定我错过了什么

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))

}

我得到的错误如下。

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

【问题讨论】:

  • 请注意,您并没有将其解组为“JSON 对象”。原始字符串是一个 JSON 对象,您将其解组为 Go 结构。

标签: json api go


【解决方案1】:

基本上有两个错误。首先是您没有将 SinglePredictions 定义为切片,这就是为什么您首先得到错误,然后在您只需要传递 []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))

}

您犯的错误似乎是认为 Go 会将第一个数组解组为 dataPredictions.SinglePredictions.Predictions。但是在dataPredictions 中,您有一个字段选择最顶层对象中的"predictions" 键,然后将其值传递给解组为*SinglePredictions

【讨论】:

  • 为什么要在 []*SinglePredictions 中添加星号 (*)?没有它似乎可以正常工作,有什么区别?
  • 有所不同,但在这种情况下,这并不重要,您可以忽略它。我只是从您问题的代码中复制了它。
【解决方案2】:

试试这个:

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))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多