遇到将文本解组为结构体时出现问题。

huangapple go评论84阅读模式
英文:

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)

https://play.golang.org/p/DFLYAfNLES

huangapple
  • 本文由 发表于 2017年2月23日 07:48:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/42404484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定