如何在golang中解析JSON而不进行两次封送处理

编程入门 行业动态 更新时间:2024-10-19 12:36:38
本文介绍了如何在golang中解析JSON而不进行两次封送处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Web套接字连接,可以在JSON对象中发送不同类型的消息,我想将内容解组为某些已知结构. 为此,我认为我应该执行以下操作:

I've got a web socket connection that sends different types of messages in a JSON object, and I want to unmarshal the contents into some known structs. To do this, I figure I should do the following:

第1步)将JSON解组到通用map [string] interface {}

Step 1) Unmarshal the JSON into a generic map[string]interface{}

第2步)找到我要寻找的钥匙

Step 2) Find the key I'm looking for

第3步)尝试将值转换为我的一种类型(失败)

Step 3) Try to cast the value into one of my types (this fails)

第3步备用)json封送该值并将其解组到我已知的结构

Step 3 alternate) json marshal this value and unmarshal it to my known struct

如果我尝试使用myStruct, ok := value.(myType),它将失败,但是如果我将json.marshal(value)再将json.unmarshal绑定到myStruct,它就可以正常工作.那是我应该怎么做的吗?进入json-> map [string] interface {}-> json-> myStruct对我来说似乎是多余的.

If I try to use myStruct, ok := value.(myType) it will fail, but if I json.marshal(value) and then json.unmarshal to myStruct, it works just fine. Is that how I'm supposed to do this? Going json-> map[string]interface{} -> json -> myStruct seems redundant to me.

示例代码:

play.golang/p/to_0Id_ja9

package main import ( "encoding/json" "fmt" ) type Ping struct { Ping string `json:"ping"` } type Ack struct { Messages []Message `json:"messages"` } type Message string func main() { testJSON := []byte(`{"ack":{"messages":["Hi there","Hi again"]}}`) var myAck = Ack{} var myMap map[string]interface{} err := json.Unmarshal(testJSON, &myMap) if err != nil { fmt.Println("error unmarshalling: ", err) } for k, v := range myMap { fmt.Printf("key: %s, value: %s \n", k, v) switch k { case "ping": fmt.Println(k, " is a ping", v) case "ack": fmt.Println(k, " is an ack containing a message list") ackjson, err := json.Marshal(v) if err != nil { fmt.Println("marshal error: ", err) } err = json.Unmarshal(ackjson, &myAck) if err != nil { fmt.Println("unmarshal error", err) } else { fmt.Println("New ack object: ", myAck) } default: fmt.Printf("%s is of a type (%T) I don't know how to handle", k, v) } } }

推荐答案

一种解决方案是通过将值解组到 json.RawMessage 而不是interface{}

One solution is to partially unmarshal the data by unmarshalling the values into a json.RawMessage instead of an interface{}

var myMap map[string]json.RawMessage

稍后仍需要进行开关,则无需编组.只要做:

Later in the switch, which still is required, you do not need to marshal. Just do:

err = json.Unmarshal(v, &myAck)

游乐场:: play.golang/p/NHd3uH5e7z

更多推荐

如何在golang中解析JSON而不进行两次封送处理

本文发布于:2023-11-25 23:53:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631838.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   而不   如何在   JSON   golang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!