遇到了解析未知键的嵌套 JSON 的问题。

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

Trouble unmarshalling nested json with unknown keys

问题

我遇到了一些问题,无法将下面格式的JSON数据解组成一个结构体。JSON的结构对我来说有点混乱,对于我做的所有愚蠢的解组尝试,我表示抱歉。

  1. {
  2. "message": {
  3. "Server1.example.com": [
  4. {
  5. "application": "Apache",
  6. "host": {
  7. "name": "/^Server-[13456]/"
  8. },
  9. "owner": "User1",
  10. "project": "Web",
  11. "subowner": "User2"
  12. }
  13. ],
  14. "Server2.example.com": [
  15. {
  16. "application": "Mysql",
  17. "host": {
  18. "name": "/^Server[23456]/"
  19. },
  20. "owner": "User2",
  21. "project": "DB",
  22. "subowner": "User3"
  23. }
  24. ]
  25. },
  26. "response_ms": 659,
  27. "success": true
  28. }

我尝试使用以下结构体进行解组:

  1. type ServerDetails struct {
  2. Message struct {
  3. Hostname struct {
  4. Details struct {
  5. Application string `json:"application"`
  6. } `json:"-"`
  7. } `json:"-"`
  8. } `json:"message"`
  9. }

在生成时,Server[0-9].example.com字段是未知的,并且会发生变化。而且,在服务器名称之后,有一个没有外部键的字段,这再次让我感到困惑。我尝试了很多组合来理解如何解组,但是失败了。

有什么有效的方法可以将JSON字段解组到结构体中吗?

英文:

I am having trouble unmarshalling a json data of the below format to a struct. The structure of the json looks a bit confusing to me, so apologies for all the dumb things I am doing to unmarshal it.

  1. {
  2. "message": {
  3. "Server1.example.com": [
  4. {
  5. "application": "Apache",
  6. "host": {
  7. "name": "/^Server-[13456]/"
  8. },
  9. "owner": "User1",
  10. "project": "Web",
  11. "subowner": "User2"
  12. }
  13. ],
  14. "Server2.example.com": [
  15. {
  16. "application": "Mysql",
  17. "host": {
  18. "name": "/^Server[23456]/"
  19. },
  20. "owner": "User2",
  21. "project": "DB",
  22. "subowner": "User3"
  23. }
  24. ]
  25. },
  26. "response_ms": 659,
  27. "success": true
  28. }

I am trying to unmarshal it using the following struct.

  1. type ServerDetails struct {
  2. Message struct{
  3. Hostname struct{
  4. Details struct{
  5. Application string `json:"application"`
  6. }`json:"-"`
  7. }`json:"-"`
  8. }`json:"message"`
  9. }

The fields Server[0-9].example.com will be unknown at the time of generating, and will change, and there is this field

  1. {
  2. "application": "Apache",
  3. "host": {
  4. "name": "/^Server-[13456]/"
  5. },

just after the server name that doesn't have a key outside, which again looks confusing to me. I tried a good number of combinations to understand how this could be unmarshalled, but I failed.

What is a working approach to get the json fields unmarshal into a struct?

答案1

得分: 3

你可以使用map[string]ServerStruct来满足你的需求。

你的结构体可以像这样定义:

  1. type YourStruct struct {
  2. Success bool
  3. ResponseMS int
  4. Servers map[string]*ServerStruct
  5. }
  6. type ServerStruct struct {
  7. Application string
  8. Owner string
  9. // 其他字段...
  10. }

通过添加一些额外的JSON标签,你就可以解析你的JSON数据了。

英文:

You could include a map[string]ServerStruct to fullfill your requirements.

your struct could look like this:

  1. type YourStruct struct {
  2. Success bool
  3. ResponseMS int
  4. Servers map[string]*ServerStruct
  5. }
  6. type ServerStruct struct {
  7. Application string
  8. Owner string
  9. [...]
  10. }

With some additional json tags, you will be able to parse your json.

答案2

得分: 0

你的JSON在第二个"]"后面有多余的逗号,不符合规范。一旦你修正了JSON,你可以使用优秀的https://mholt.github.io/json-to-go/来构建以下Go结构体:

  1. type AutoGenerated struct {
  2. Message struct {
  3. Server1ExampleCom []struct {
  4. Application string `json:"application"`
  5. Host struct {
  6. Name string `json:"name"`
  7. } `json:"host"`
  8. Owner string `json:"owner"`
  9. Project string `json:"project"`
  10. Subowner string `json:"subowner"`
  11. } `json:"Server1.example.com"`
  12. Server2ExampleCom []struct {
  13. Application string `json:"application"`
  14. Host struct {
  15. Name string `json:"name"`
  16. } `json:"host"`
  17. Owner string `json:"owner"`
  18. Project string `json:"project"`
  19. Subowner string `json:"subowner"`
  20. } `json:"Server2.example.com"`
  21. } `json:"message"`
  22. ResponseMs int `json:"response_ms"`
  23. Success bool `json:"success"`
  24. }
英文:

You JSON is not valid with superfluous comma after the second ]
Once you correct the JSON, you can use the excellent https://mholt.github.io/json-to-go/ to build the following Go struct

  1. type AutoGenerated struct {
  2. Message struct {
  3. Server1ExampleCom []struct {
  4. Application string `json:"application"`
  5. Host struct {
  6. Name string `json:"name"`
  7. } `json:"host"`
  8. Owner string `json:"owner"`
  9. Project string `json:"project"`
  10. Subowner string `json:"subowner"`
  11. } `json:"Server1.example.com"`
  12. Server2ExampleCom []struct {
  13. Application string `json:"application"`
  14. Host struct {
  15. Name string `json:"name"`
  16. } `json:"host"`
  17. Owner string `json:"owner"`
  18. Project string `json:"project"`
  19. Subowner string `json:"subowner"`
  20. } `json:"Server2.example.com"`
  21. } `json:"message"`
  22. ResponseMs int `json:"response_ms"`
  23. Success bool `json:"success"`
  24. }

huangapple
  • 本文由 发表于 2016年11月9日 19:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/40505410.html
匿名

发表评论

匿名网友

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

确定