Go 数据类型struct问题

需求

如下的数据结构中,创建一个struct数据结构。

{
  "event": {
    "header": {
      "namespace":"Alexa.Discovery",
      "name":"Discover.Response",
      "payloadVersion": "3",
      "messageId": "<message id>"
    },
    "payload":{
      "endpoints":[
        {
            "endpointId": "<unique ID of the endpoint>",
            "manufacturerName": "<the manufacturer name of the endpoint>",
            "modelName": "<the model name of the endpoint>",
            "description": "<a description that is shown in the Alexa app>",                      "friendlyName": "Front door camera",
            "displayCategories": [ "CAMERA" ],
          "cookie": {
          },
          "capabilities":
          [
            {
              "type": "AlexaInterface",
              "interface": "Alexa.CameraStreamController",
              "version": "3",
              "cameraStreamConfigurations" : [
                  {
                    "protocols": ["RTSP"],
                    "resolutions": [{"width":1920, "height":1080}, {"width":1280, "height":720}],
                    "authorizationTypes": ["BASIC"],
                    "videoCodecs": ["H264", "MPEG2"],
                    "audioCodecs": ["G711"]
                  },
                  {
                    "protocols": ["RTSP"],
                    "resolutions": [{"width":1920, "height":1080}, {"width":1280, "height":720}],
                    "authorizationTypes": ["NONE"],
                    "videoCodecs": ["H264"],
                    "audioCodecs": ["AAC"]
                 }
              ]
            },
            {
              "type": "AlexaInterface",
              "interface": "Alexa.PowerController",
              "version": "3"
            },
            {
              "type": "AlexaInterface",
              "interface": "Alexa.EndpointHealth",
              "version": "3",
              "properties": {
                "supported": [
                  {
                    "name": "connectivity"
                  }
                ],
                "proactivelyReported": true,
                "retrievable": true
              }
            }
          ]
        }
      ]
    }
  }
}

问题

capabilities 里的数据有部分是存在的,但并不是每一个都有cameraStreamConfigurations,如何创建 capabilities的数据结构?capabilities []Capability ?

type Endpoint struct {
    capabilities []Capability `json:"capabilities"`
}

type Capability struct {
    Type       string               `json:"type"`
    Interface  string               `json:"interface"`
    Version    string               `json:"version"`
    Properties CapabilityProperties `json:"properties"`
}
阅读 1.7k
1 个回答

并不一定有,就可忽略就好。omitempty

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进