简体   繁体   English

JSON在嵌套结构中解组

[英]JSON unmarshal in nested struct

I am trying to unmarshal an incoming JSON into a struct that contains an array of structs. 我试图将传入的JSON解组为包含结构数组的结构。 However I get the error 但是我得到了错误

"Invalid input. JSON badly formatted. json: cannot unmarshal array into Go struct field DataInput.Asset of type app.AssetStorage" “输入无效.JSON格式错误.json:无法将数组解组为Go结构字段DataInput.Asset,类型为app.AssetStorage”

I have tried to recreate the code here: https://play.golang.org/p/RuBaBjPmWxO , however I can't reproduce the error (although the incoming message and code are identical). 我试图在这里重新创建代码: https//play.golang.org/p/RuBaBjPmWxO ,但我无法重现错误(尽管传入的消息和代码是相同的)。

type AssetStorage struct {
    Event         string   `json:"Event"`
    EmployeeID    int      `json:"EmployeeID"`
    EmployeeEmail string   `json:"EmployeeEmail"`
    PerformedBy   string   `json:"PerformedBy"`
    Timestamp     string   `json:"Timestamp"`
    AlgorithmID   string   `json:"AlgorithmID"`
    AlgorithmHash string   `json:"AlgorithmHash"`
    Objects       []Object `json:"Objects"`
}

type Object struct {
    ShortName   string `json:"ShortName"`
    Hash        string `json:"Hash"`
    DestroyDate string `json:"DestroyDate"`
}

type DataInput struct {
    Username string
    Token    string       `json:"Token"`
    Asset    AssetStorage `json:"Asset"`
}

func main() {
    var data DataInput
    json.Unmarshal(input, data)
    data.Username = data.Asset.EmployeeEmail
    fmt.Printf("%+v\n", data)
}

There are three bugs in your code, one is that your are not using address of DataInput struct when you are unmarshalling your JSON. 您的代码中有三个错误,一个是当您解组JSON时,您没有使用DataInput结构的地址。

This should be: 这应该是:

var data DataInput
json.Unmarshal(input, data)

like below: 如下:

var data DataInput
if err := json.Unmarshal(input, &data); err != nil {
    log.Println(err)
}

One Piece of advice in above code. 以上代码中的一条建议。 Never skip errors to know more about the error 切勿跳过错误以了解有关错误的更多信息

Next as the error says: 接下来的错误是:

Invalid input. 输入无效。 JSON badly formatted. JSON格式错误。 json: cannot unmarshal array into Go struct field DataInput.Asset of type app.AssetStorage json:无法将数组解组为类型为app.AssetStorage的Go结构字段DataInput.Asset

DataInput.Asset should be an array of json objects there you should change your AssetStorage to []AssetStorage in your declaration in DataInput struct. DataInput.Asset应该是JSON对象的数组有你需要改变你AssetStorage[]AssetStorage在你的宣言DataInput结构。

One more error is that you are declaring the type of EmployeeID field for AssetStorage struct to be an int which should be a string 还有一个错误是你将AssetStorage结构的EmployeeID字段的类型声明为一个int ,它应该是一个string

Working Code on Go Playground 去游乐场的工作守则

The answer is in the error message: 答案在错误消息中:

Invalid input. 输入无效。 JSON badly formatted. JSON格式错误。 json: cannot unmarshal array into Go struct field DataInput.Asset of type app.AssetStorage json:无法将数组解组为类型为app.AssetStorage的Go结构字段DataInput.Asset

The json you're parsing has DataInput.Asset as an array of AssetStorage objects. 您正在解析的json将DataInput.Asset作为AssetStorage对象的数组 So, the type needs to be []AssetStorage . 所以,类型需要是[]AssetStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM