go中的类型嵌套json解码[复制]

时间:2022-10-17 13:20:33

This question already has an answer here:

这个问题在这里已有答案:

I try to Unmarshal some nested JSON in go, the structs go something along this

我尝试在go中解组一些嵌套的JSON,结构就是这样的

type GameState struct {
  missiles []*Missile
  others   []*Player
  you      *Player
}
type Message struct {
  gamestate   *GameState
  messagetype string
}
// json like {"gamestate":{...},"messagetype":"stateupdate"}

I've also put a running example online.

我还在线提供了一个运行示例。

Now when I use map[string]interface{} to Unmarshal everything gets deserialized. But the moment I use the structs to Unmarshal I get nil back, both as result and error.

现在,当我使用map [string] interface {}来解组时,所有内容都会被反序列化。但是当我使用结构组织Unmarshal时,我得到了nil,结果和错误。

How come?

怎么来的?

1 个解决方案

#1


2  

All struct fields you want to marshal should start with uppercase letter (be public). This works https://play.golang.org/p/ReCp3BAfTb

您要编组的所有结构字段都应以大写字母开头(是公共的)。这适用于https://play.golang.org/p/ReCp3BAfTb

#1


2  

All struct fields you want to marshal should start with uppercase letter (be public). This works https://play.golang.org/p/ReCp3BAfTb

您要编组的所有结构字段都应以大写字母开头(是公共的)。这适用于https://play.golang.org/p/ReCp3BAfTb