英文:
Trouble unmarshaling text into struct
问题
尝试将一个 JSON 文本解组为自定义的结构体。我的结构体定义似乎是正确的,但 json.Unmarshal 没有返回任何内容。
package main
import (
	"encoding/json"
	"fmt"
)
type CmdUnit struct {
	Command     string
	Description string
}
type CmdList struct {
	ListOfCommands []CmdUnit
}
type OneCmdList struct {
	Area    string
	CmdList CmdList
}
type AllCommands struct {
	AllAreas []OneCmdList
}
func main() {
	jsonTxt := `
	{
        "Area1": [{
                "Command": "cmd1",
                "Desc": "cmd1 desc"
        }, {
                "Command": "cmd2",
                "Desc": "cmd2 desc"
        }],
        "Area2": [{
                "Command": "cmd1",
                "Desc": "cmd1 desc"
        }]
}
`
	cmds := AllCommands{}
	if err := json.Unmarshal([]byte(jsonTxt), &cmds); err != nil {
		fmt.Println("Failed to unmarshal:", err)
	} else {
		fmt.Printf("%+v\n", cmds)
	}
}
$ go run j.go
{AllAreas:[]}
以上是你要翻译的内容。
英文:
Trying to unmarshal a json text into my own struct. My struct definitions seem correct but json.Unmarshal doesn't return anything.
package main
import (
	"encoding/json"
	"fmt"
)
type CmdUnit struct {
	Command     string
	Description string
}
type CmdList struct {
	ListOfCommands []CmdUnit
}
type OneCmdList struct {
	Area    string
	CmdList CmdList
}
type AllCommands struct {
	AllAreas []OneCmdList
}
func main() {
	jsonTxt := `
	{
        "Area1": [{
                "Command": "cmd1",
                "Desc": "cmd1 desc"
        }, {
                "Command": "cmd2",
                "Desc": "cmd2 desc"
        }],
        "Area2": [{
                "Command": "cmd1",
                "Desc": "cmd1 desc"
        }]
}
`
	cmds := AllCommands{}
	if err := json.Unmarshal([]byte(jsonTxt), &cmds); err != nil {
		fmt.Println("Failed to unmarshal:", err)
	} else {
		fmt.Printf("%+v\n", cmds)
	}
}
$ go run j.go
{AllAreas:[]}
答案1
得分: 3
你的结构体与你提供的 JSON 有不同的结构。在你的示例中,将结构体编组将导致以下 JSON 结构:
{
  "AllAreas": [
    {
      "Area": "Area1",
      "CmdList": {
        "ListOfCommands": [
          {
            "Command": "cmd1",
            "Description": "cmd1 desc"
          },
          {
            "Command": "cmd2",
            "Description": "cmd2 desc"
          }
        ]
      }
    }
  ]
}
在你的示例中,可以直接将 JSON 反编组为 map[string][]CmdUnit{},只需将 CmdUnit.Description 更改为 CmdUnit.Desc。
cmds := map[string][]CmdUnit{}
if err := json.Unmarshal(jsonTxt, &cmds); err != nil {
    log.Fatal("Failed to unmarshal:", err)
}
fmt.Printf("%+v\n", cmds)
链接:https://play.golang.org/p/DFLYAfNLES
英文:
Your structs have a different structure from the json you're providing. Marshalling the structs in your example would result in json that looks like:
{
  "AllAreas": [
    {
      "Area": "Area1",
      "CmdList": {
        "ListOfCommands": [
          {
            "Command": "cmd1",
            "Description": "cmd1 desc"
          },
          {
            "Command": "cmd2",
            "Description": "cmd2 desc"
          }
        ]
      }
    }
  ]
}
The json in your example can be unmarshaled directly into a map[string][]CmdUnit{} with the minor change of CmdUnit.Description to CmdUnit.Desc.
cmds := map[string][]CmdUnit{}
if err := json.Unmarshal(jsonTxt, &cmds); err != nil {
	log.Fatal("Failed to unmarshal:", err)
}
fmt.Printf("%+v\n", cmds)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论