使用Golang解析嵌套的JSON对象。

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

Parse nested json object using golang

问题

我想从Results中的第一个元素中访问result_rows。到目前为止,我已经成功访问了output->Results->Name,但无法进一步操作。有什么提示吗?

以下是完整的代码:

package main

import (
	"encoding/json"
	"fmt"
)

type PoliticsJson struct {
	Status string
	Output []struct {
		Name    string
		Results []struct {
			Name           string
			Result_Headers []string
		}
	}
}

func main() {
	s := `{
  "status": "pass",
  "output": [
    {
      "Name": "example",
      "ExecutionTime": "2021-08-22T10:23:10.775986761Z",
      "Passed": false,
      "Results": [
        {
          "name": "name1",
          "description": "desc1",
          "result_headers": [
            "name",
            "types",
            "times"
          ],
          "result_rows": [
            [
              "insta-a",
              {
                "Elements": [
                  "x-aaaa",
                  "x-bbbb"
                ],
                "Dimensions": [
                  {
                    "Length": 2,
                    "LowerBound": 1
                  }
                ],
                "Status": 2
              },
              {
                "Elements": [
                  {
                    "Time": "2021-07-28T20:01:47Z",
                    "Status": 2,
                    "InfinityModifier": 0
                  },
                  {
                    "Time": "2021-07-28T19:24:33Z",
                    "Status": 2,
                    "InfinityModifier": 0
                  }
                ],
                "Dimensions": [
                  {
                    "Length": 2,
                    "LowerBound": 1
                  }
                ],
                "Status": 2
              }
            ],
            [
              "insta-b",
              {
                "Elements": [
                  "x-eeee",
                  "x-ffff"
                ],
                "Dimensions": [
                  {
                    "Length": 2,
                    "LowerBound": 1
                  }
                ],
                "Status": 2
              },
              {
                "Elements": [
                  {
                    "Time": "2021-07-18T06:18:15Z",
                    "Status": 2,
                    "InfinityModifier": 0
                  },
                  {
                    "Time": "2021-08-17T12:20:33Z",
                    "Status": 2,
                    "InfinityModifier": 0
                  }
                ],
                "Dimensions": [
                  {
                    "Length": 2,
                    "LowerBound": 1
                  }
                ],
                "Status": 2
              }
            ],
            [
              "insta-c",
              {
                "Elements": [
                  "x-tttt",
                  "x-yyyy"
                ],
                "Dimensions": [
                  {
                    "Length": 2,
                    "LowerBound": 1
                  }
                ],
                "Status": 2
              },
              {
                "Elements": [
                  {
                    "Time": "2021-08-10T07:57:32Z",
                    "Status": 2,
                    "InfinityModifier": 0
                  },
                  {
                    "Time": "2021-08-10T07:56:45Z",
                    "Status": 2,
                    "InfinityModifier": 0
                  }
                ],
                "Dimensions": [
                  {
                    "Length": 2,
                    "LowerBound": 1
                  }
                ],
                "Status": 2
              }
            ]
          ],
          "type": "manual",
          "check_passed": false
        },
        {
          "name": "name2",
          "description": "desc2",
          "result_headers": [
            "id",
            "inserted_at",
            "updated_at"
          ],
          "result_rows": [
            [
              120553,
              "2021-04-16T00:50:58.159354Z",
              "2021-08-01T19:06:05.130543Z"
            ],
            [
              72601,
              "2021-12-10T17:03:53.006288Z",
              "2021-08-03T20:43:52.248167Z"
            ]
          ],
          "type": "manual",
          "check_passed": false
        },
        {
          "name": "name3",
          "description": "desc3",
          "result_headers": [
            "id",
            "inserted_at",
            "updated_at"
          ],
          "result_rows": [
            [
              476287,
              "2021-07-06T11:25:19.988087Z",
              "2021-07-06T11:25:19.988087Z"
            ],
            [
              499012,
              "2021-08-12T02:28:51.993078Z",
              "2021-08-12T02:28:51.993078Z"
            ]
          ],
          "type": "manual",
          "check_passed": false
        },
        {
          "name": "name4",
          "description": "desc4",
          "result_headers": [
            "id",
            "inserted_at",
            "updated_at",
            "organization_id"
          ],
          "result_rows": [
            [
              172221,
              "2021-08-17T05:24:01.350184Z",
              "2021-08-17T05:24:48.869273Z",
              140902
            ],
            [
              171968,
              "2021-08-16T18:35:55.03086Z",
              "2021-08-16T18:36:00.255017Z",
              140686
            ]
          ],
          "type": "manual",
          "check_passed": false
        },
        {
          "name": "name5",
          "description": "desc-5",
          "result_headers": [
            "id",
            "inserted_at",
            "updated_at"
          ],
          "result_rows": [
            [
              232531,
              "2021-10-01T23:30:59.965772Z",
              "2021-10-01T23:30:59.965772Z"
            ],
            [
              232642,
              "2021-10-02T13:38:22.923795Z",
              "2021-10-02T13:38:22.923795Z"
            ]
          ],
          "type": "manual",
          "check_passed": false
        }
      ],
      "Error": "",
      "Loaded": null
    }
  ]
}`

	var p PoliticsJson

	err := json.Unmarshal([]byte(s), &p)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s \n", p.Output[0].Results[0].Name)
}

你可以在这里运行代码:https://go.dev/play/p/9-gjQLTD7Ao

英文:

I want to access result_rows from first element inside Results. So far I managed to access, output->Results->Name but unable to proceed further. Any hints?

/* Required outputs from result_rows:
output-> Results -> result_rows -> insta-a  x-aaaa,x-bbbb
output-> Results -> result_rows -> insta-c x-eeee, x-ffff
output-> Results -> result_rows -> insta-c x-tttt, x-yyyy 
*/

Here is the complete code:

package main
import (
"encoding/json"
"fmt"
)
type PoliticsJson struct {
Status string
Output []struct {
Name    string
Results []struct {
Name           string
Result_Headers []string
}
}
}
func main() {
s := `{
"status": "pass",
"output": [
{
"Name": "example",
"ExecutionTime": "2021-08-22T10:23:10.775986761Z",
"Passed": false,
"Results": [
{
"name": "name1",
"description": "desc1",
"result_headers": [
"name",
"types",
"times"
],
"result_rows": [
[
"insta-a",
{
"Elements": [
"x-aaaa",
"x-bbbb"
],
"Dimensions": [
{
"Length": 2,
"LowerBound": 1
}
],
"Status": 2
},
{
"Elements": [
{
"Time": "2021-07-28T20:01:47Z",
"Status": 2,
"InfinityModifier": 0
},
{
"Time": "2021-07-28T19:24:33Z",
"Status": 2,
"InfinityModifier": 0
}
],
"Dimensions": [
{
"Length": 2,
"LowerBound": 1
}
],
"Status": 2
}
],
[
"insta-b",
{
"Elements": [
"x-eeee",
"x-ffff"
],
"Dimensions": [
{
"Length": 2,
"LowerBound": 1
}
],
"Status": 2
},
{
"Elements": [
{
"Time": "2021-07-18T06:18:15Z",
"Status": 2,
"InfinityModifier": 0
},
{
"Time": "2021-08-17T12:20:33Z",
"Status": 2,
"InfinityModifier": 0
}
],
"Dimensions": [
{
"Length": 2,
"LowerBound": 1
}
],
"Status": 2
}
],
[
"insta-c",
{
"Elements": [
"x-tttt",
"x-yyyy"
],
"Dimensions": [
{
"Length": 2,
"LowerBound": 1
}
],
"Status": 2
},
{
"Elements": [
{
"Time": "2021-08-10T07:57:32Z",
"Status": 2,
"InfinityModifier": 0
},
{
"Time": "2021-08-10T07:56:45Z",
"Status": 2,
"InfinityModifier": 0
}
],
"Dimensions": [
{
"Length": 2,
"LowerBound": 1
}
],
"Status": 2
}
]
],
"type": "manual",
"check_passed": false
},
{
"name": "name2",
"description": "desc2",
"result_headers": [
"id",
"inserted_at",
"updated_at"
],
"result_rows": [
[
120553,
"2021-04-16T00:50:58.159354Z",
"2021-08-01T19:06:05.130543Z"
],
[
72601,
"2021-12-10T17:03:53.006288Z",
"2021-08-03T20:43:52.248167Z"
]
],
"type": "manual",
"check_passed": false
},
{
"name": "name3",
"description": "desc3",
"result_headers": [
"id",
"inserted_at",
"updated_at"
],
"result_rows": [
[
476287,
"2021-07-06T11:25:19.988087Z",
"2021-07-06T11:25:19.988087Z"
],
[
499012,
"2021-08-12T02:28:51.993078Z",
"2021-08-12T02:28:51.993078Z"
]
],
"type": "manual",
"check_passed": false
},
{
"name": "name4",
"description": "desc4",
"result_headers": [
"id",
"inserted_at",
"updated_at",
"organization_id"
],
"result_rows": [
[
172221,
"2021-08-17T05:24:01.350184Z",
"2021-08-17T05:24:48.869273Z",
140902
],
[
171968,
"2021-08-16T18:35:55.03086Z",
"2021-08-16T18:36:00.255017Z",
140686
]
],
"type": "manual",
"check_passed": false
},
{
"name": "name5",
"description": "desc-5",
"result_headers": [
"id",
"inserted_at",
"updated_at"
],
"result_rows": [
[
232531,
"2021-10-01T23:30:59.965772Z",
"2021-10-01T23:30:59.965772Z"
],
[
232642,
"2021-10-02T13:38:22.923795Z",
"2021-10-02T13:38:22.923795Z"
]
],
"type": "manual",
"check_passed": false
}
],
"Error": "",
"Loaded": null
}
]
}
`
var p PoliticsJson
err := json.Unmarshal([]byte(s), &p)
if err != nil {
panic(err)
}
fmt.Printf("%s \n", p.Output[0].Results[0].Name)
}

https://go.dev/play/p/9-gjQLTD7Ao

答案1

得分: 1

请使用以下结构来解析JSON:

type PoliticsJson struct {
	Status string `json:"status"`
	Output []struct {
		Name          string    `json:"Name"`
		ExecutionTime time.Time `json:"ExecutionTime"`
		Passed        bool      `json:"Passed"`
		Results       []struct {
			Name          string          `json:"name"`
			Description   string          `json:"description"`
			ResultHeaders []string        `json:"result_headers"`
			ResultRows    [][]interface{} `json:"result_rows"`
			Type          string          `json:"type"`
			CheckPassed   bool            `json:"check_passed"`
		} `json:"Results"`
		Error  string      `json:"Error"`
		Loaded interface{} `json:"Loaded"`
	} `json:"output"`
}

请注意,ResultRows[][]interface{} 类型,因为该数组包含多种类型的数据。

英文:

Use the following structure to unmarshal the JSON

type PoliticsJson struct {
	Status string `json:"status"`
	Output []struct {
		Name          string    `json:"Name"`
		ExecutionTime time.Time `json:"ExecutionTime"`
		Passed        bool      `json:"Passed"`
		Results       []struct {
			Name          string          `json:"name"`
			Description   string          `json:"description"`
			ResultHeaders []string        `json:"result_headers"`
			ResultRows    [][]interface{} `json:"result_rows"`
			Type          string          `json:"type"`
			CheckPassed   bool            `json:"check_passed"`
		} `json:"Results"`
		Error  string      `json:"Error"`
		Loaded interface{} `json:"Loaded"`
	} `json:"output"`
}

Note that ResultRows is [][]interface{} because the array contains data of multiple types.

huangapple
  • 本文由 发表于 2022年9月8日 01:28:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/73639511.html
匿名

发表评论

匿名网友

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

确定