如何在golang中解析JSON而不进行两次封送处理 [英] How to parse JSON in golang without unmarshaling twice

查看:56
本文介绍了如何在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.

示例代码:

https://play.golang.org/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)
        }
    }
}

推荐答案

一种解决方案是通过将值解组到

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)

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

这篇关于如何在golang中解析JSON而不进行两次封送处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆