-3

我是 Go 新手,一直在解组 json 数据。在解组 JSON 以构造作为响应返回的响应 json 时遇到麻烦。

一对内部变量的键是匿名的。因此,对它们进行解组不会提供任何数据。

Json响应如下

{
  "data": [
    {
      "Name": "Dummy Value",
      "configEvents": {
        "5f94f412-532a-41a8-821e-6326dd72556b": {
          "eventId": "dummy",
          "start": "dummy",
          "end": "dummy",
          "details": {
            "providerCallSign": "dummy",
            "callSign": "dummy"
          }
        }
      },
      "policyEvents": {
        "930195be-f409-421c-abe2-a39d2450d669": {
          "details": {
            "daiMobileFlag": false
          }
        }
      }
    }
  ]
}

我的结构如下

type Response struct {
    Data []struct {
        Name         string `json:"Name"`
        ConfigEvents struct {
            FiveF94F412532A41A8821E6326Dd72556B struct {
                EventID string `json:"eventId"`
                Start   string `json:"start"`
                End     string `json:"end"`
                Details struct {
                    ProviderCallSign string `json:"providerCallSign"`
                    CallSign         string `json:"callSign"`
                } `json:"details"`
            } `json:"5f94f412-532a-41a8-821e-6326dd72556b"`
        } `json:"configEvents"`
        PolicyEvents struct {
            Nine30195BeF409421CAbe2A39D2450D669 struct {
                Details struct {
                    DaiMobileFlag bool `json:"daiMobileFlag"`
                } `json:"details"`
            } `json:"930195be-f409-421c-abe2-a39d2450d669"`
        } `json:"policyEvents"`
    } `json:"data"`
}

由于响应中的键是动态的,因此解组不会捕获该特定对象。有什么方法可以在 goLang 中处理这种情况。

4

1 回答 1

1

我将json固定为类似这样有效的东西

    {
  "data": [
    {
      "Name": "Dummy Value",
      "configEvents": {
        "5f94f412-532a-41a8-821e-6326dd72556b": {
          "eventId": "dummy",
          "start": "dummy",
          "end": "dummy",
          "details": {
            "providerCallSign": "dummy",
            "callSign": "dummy"
          }
        }
      },
      "policyEvents": {
        "930195be-f409-421c-abe2-a39d2450d669": {
          "details": {
            "daiMobileFlag": false
          }
        }
      }
    }
  ]
}

然后这段代码可以摄取它

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
)

type insideConfigEvents struct {
    EventID string `json:"eventId"`
    Start   string `json:"start"`
    End     string `json:"end"`
    Details struct {
        ProviderCallSign string `json:"providerCallSign"`
        CallSign         string `json:"callSign"`
    } `json:"details"`
}

type insidePolicyEvents struct {
    Details struct {
        DaiMobileFlag bool `json:"daiMobileFlag"`
    } `json:"details"`
}

type Input struct {
    Data []struct {
        Name         string                        `json:"Name"`
        ConfigEvents map[string]insideConfigEvents `json:"configEvents"`
        PolicyEvents map[string]insidePolicyEvents `json:"policyEvents"`
    } `json:"data"`
}

func main() {
    var dat Input
    b, err := ioutil.ReadFile("inv.json") // just pass the file name
    if err != nil {
        fmt.Print(err)
    }
    if err := json.Unmarshal(b, &dat); err != nil {
        panic(err)
    }
    fmt.Println(dat.Data[0].ConfigEvents)
}
于 2020-12-18T21:43:21.250 回答