在 Go 中解组 JSON 标记联合

Tim*_*mmm 7 json go unmarshalling

我正在尝试对Google Actions的 JSON 请求进行解组。这些有标记联合数组,如下所示:

{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
      "intent": "action.devices.QUERY",
      "payload": {
        "devices": [{
          "id": "123",
          "customData": {
            "fooValue": 74,
            "barValue": true,
            "bazValue": "foo"
          }
        }, {
          "id": "456",
          "customData": {
            "fooValue": 12,
            "barValue": false,
            "bazValue": "bar"
          }
        }]
      }
    }]
}

{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
      "intent": "action.devices.EXECUTE",
      "payload": {
        "commands": [{
          "devices": [{
            "id": "123",
            "customData": {
              "fooValue": 74,
              "barValue": true,
              "bazValue": "sheepdip"
            }
          }, {
            "id": "456",
            "customData": {
              "fooValue": 36,
              "barValue": false,
              "bazValue": "moarsheep"
            }
          }],
          "execution": [{
            "command": "action.devices.commands.OnOff",
            "params": {
              "on": true
            }
          }]
        }]
      }
    }]
}

etc.
Run Code Online (Sandbox Code Playgroud)

显然,我可以将其解组为 aninterface{}并使用完全动态类型转换和所有内容来解码它,但是 Go 对解码结构有很好的支持。有没有办法在 Go 中优雅地做到这一点(例如,就像在 Rust 中一样)?

我觉得你几乎可以通过最初阅读解组来做到这一点:

type Request struct {
    RequestId string
    Inputs    []struct {
        Intent   string
        Payload  interface{}
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,一旦你拥有了,Payload interface{}似乎没有任何方法可以将其反序列化为 a struct(除了序列化它并再次反序列化它,这很糟糕。有什么好的解决方案吗?

Iai*_*can 8

您可以将其存储为 a Payload,然后根据 Intent 的值对其进行解组,而不是解组到 an 。这在 json 文档的示例中显示:interface{}json.RawMessage

https://golang.org/pkg/encoding/json/#example_RawMessage_unmarshal

将该示例与您的 JSON 一起使用,并构造您的代码,如下所示:

type Request struct {
    RequestId string
    Inputs    []struct {
        Intent   string
        Payload  json.RawMessage
    }
}

var request Request
err := json.Unmarshal(j, &request)
if err != nil {
    log.Fatalln("error:", err)
}
for _, input := range request.Inputs {
    var payload interface{}
    switch input.Intent {
    case "action.devices.EXECUTE":
        payload = new(Execute)
    case "action.devices.QUERY":
        payload = new(Query)
    }
    err := json.Unmarshal(input.Payload, payload)
    if err != nil {
        log.Fatalln("error:", err)
    }
    // Do stuff with payload
}
Run Code Online (Sandbox Code Playgroud)