繁体   English   中英

使用 golang 解析嵌套的 json object

[英]Parse nested json object using golang

我想从Results中的第一个元素访问result_rows 到目前为止,我设法访问了output->Results->Name但无法继续进行。 有什么提示吗?

/* 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 
*/

这是完整的代码:

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

使用以下结构解组 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{}因为数组包含多种类型的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM