取消具有未知字段名称但已知结构的 JSON

我从traefik tls中检索acme.json,其中traefik存储ssl / tls证书信息。


现在我想用golang将acme.json解组到我的go结构“Traefik”中。但是我不知道如何处理动态/未知的json字段名称,因为和是我在编译时不知道的名称。这些名称应在 go 中动态配置。certificateresolver1certificateresolver2


我知道json的结构(它总是一样的),但不知道证书解析器的字段名称。


有谁知道做到这一点的最佳方法吗?


Traefik acme.json

{

  "certificateresolver1": {

    "Account": {

      "Email": "email@example.com",

      "Registration": {

        "body": {

          "status": "valid",

          "contact": [

            "mailto:email@example.com"

          ]

        },

        "uri": "https://acme-v02.api.letsencrypt.org/acme/acct/124448363"

      },

      "PrivateKey": "PRIVATEKEY",

      "KeyType": "4096"

    },

    "Certificates": [

      {

        "domain": {

          "main": "example.com",

          "sans": [

            "test.example.com"

          ]

        },

        "certificate": "CERTIFICATE",

        "key": "KEY",

        "Store": "default"

      },

      {

        "domain": {

          "main": "example.org"

        },

        "certificate": "CERTIFICATE",

        "key": "KEY",

        "Store": "default"

      }

    ]

  },

  "certificateresolver2": {

    "Account": {

      "Email": "email@example.com",

      "Registration": {

        "body": {

          "status": "valid",

          "contact": [

            "mailto:email@example.com"

          ]

        },

        "uri": "https://acme-v02.api.letsencrypt.org/acme/acct/126945414"

      },

      "PrivateKey": "PRIVATEKEY",

      "KeyType": "4096"

    },

    "Certificates": [

      {

        "domain": {

          "main": "example.net"

        },

        "certificate": "CERTIFICATE",

        "key": "KEY",

        "Store": "default"

      }

    ]

  }

}


哈士奇WWW
浏览 96回答 1
1回答

繁星coding

我认为这样的事情会对你有所帮助:type ProviderMdl map[string]Providertype Provider struct {    Account struct {        Email        string `json:"Email"`        Registration struct {            Body struct {                Status  string   `json:"status"`                Contact []string `json:"contact"`            } `json:"body"`            URI string `json:"uri"`        } `json:"Registration"`        PrivateKey string `json:"PrivateKey"`        KeyType    string `json:"KeyType"`    } `json:"Account"`    Certificates []struct {        Domain struct {            Main string   `json:"main"`            Sans []string `json:"sans"`        } `json:"domain"`        Certificate string `json:"certificate"`        Key         string `json:"key"`        Store       string `json:"Store"`    } `json:"Certificates"`}因此,您可以通过以下方式处理此数据:    bres := new(ProviderMdl)    if err := json.Unmarshal(data, bres); err != nil {        panic(err)    }        // fmt.Printf("%+v - \n", bres)    for key, value := range *bres {        fmt.Printf("%v - %v\n", key, value)    }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go