登录
首页 >  Golang >  Go问答

Go语言中解析JSON标签结构

来源:stackoverflow

时间:2024-03-05 22:36:27 159浏览 收藏

Golang不知道大家是否熟悉?今天我将给大家介绍《Go语言中解析JSON标签结构》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

问题内容

我正在尝试对 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.

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

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

type Request struct {
    RequestId string
    Inputs    []struct {
        Intent   string
        Payload  interface{}
    }
}

但是,一旦您有了 payload 接口{},似乎就没有任何方法可以将其反序列化为 struct(除了序列化并再次反序列化,这很糟糕。有什么好的解决方案吗?


解决方案


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

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
}

正是出于这个原因,我制作了 https://github.com/byrnedo/pjson

所以在你的情况下你会:

type Input interface {
    // Variant func is required
    Variant() string
}

type ExecuteInput struct {
    Payload struct {
        // is any just to avoid typing it for this example
        Commands []any `json:"commands"`
    } `json:"payload"`
}

func (q ExecuteInput) Variant() string {
    return "action.devices.EXECUTE"
}

type QueryInput struct {
    Payload struct {
        // is any just to avoid typing it for this example
        Devices []any `json:"devices"`
    } `json:"payload"`
}

func (q QueryInput) Variant() string {
    return "action.devices.QUERY"
}

type Inputs []Input

func (i Inputs) MarshalJSON() ([]byte, error) {
    return pjson.New(Inputs{}, pjson.WithVariantField("intent")).MarshalArray(i)
}

func (i *Inputs) UnmarshalJSON(bytes []byte) (err error) {
    *i, err = pjson.New(Inputs{ExecuteInput{}, QueryInput{}}, pjson.WithVariantField("intent")).UnmarshalArray(bytes)
    return
}

type Request struct {
    RequestId string `json:"requestId"`
    Inputs    Inputs `json:"inputs"`
}

Try it on go playground

到这里,我们也就讲完了《Go语言中解析JSON标签结构》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>