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"` }
到这里,我们也就讲完了《Go语言中解析JSON标签结构》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习