为了账号安全,请及时绑定邮箱和手机立即绑定

在 Golang 中解组具有重叠字段的动态 JSON 数据

在 Golang 中解组具有重叠字段的动态 JSON 数据

Go
守候你守候我 2022-11-28 16:49:40
抱歉,如果我发布的问题已经得到解答,但我似乎无法在这里找到任何类似的情况。我有一个 websocket 客户端,它接收具有重叠字段的动态 json 数据。字段重叠的事实使解组对我来说非常困难。我有我收到的数据类型的结构,但我需要一种方法来检查 json 数据,然后再将其解组为特定结构。我希望接口可以充当临时持有者,然后我可以将接口与我想要解组的特定结构相匹配,但这似乎不可能,或者我只是不知道该怎么做关于它。以下是我正在接收的数据类型的一些示例,以及与它一起使用的结构,以备不时之需。response 1: {"connectionID":17973829270596587247,"event":"systemStatus","status":"online","version":"1.9.0"}response 2: {"channelID":328,"channelName":"ohlc-5","event":"subscriptionStatus","pair":"XBT/USD","status":"subscribed","subscription":{"interval":5,"name":"ohlc"}}response 3: [328,["1649576721.042916","1649577000.000000","42641.50000","42641.50000","42641.50000","42641.50000","42641.50000","0.00335101",2],"ohlc-5","XBT/USD"]response 4: {"event":"heartbeat"}structs belowimport (    "time"    "encoding/json")type ConnStatus struct {    ConnectionID        uint64          `json:"connectionID"`    Event               string          `json:"event"`    Status              string          `json:"status"`    Version             string          `json:"version"`}type HeartBeat struct {    Event               string          `json:"event"`}type OHLCsuccess struct {    ChannelID           int             `json:"channelID"`    ChannelName         string          `json:"channelName"`    Event               string          `json:"event"`    Pair                string          `json:"pair"`    Status              string          `json:"status"`    Subscription        OHLC            `json:"subscription"`}type OHLC struct {    Interval        int         `json:"interval"`    Name            string      `json:"name"`}type OHLCUpdates struct {    ChannelID           int    OHLCArray           OHLCNewTrade    ChannelName         string    Pair                string}关于如何解决这个问题的任何想法?在此先感谢您的帮助!
查看完整描述

1 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

你能控制不同的反应吗?如果是这样,要不要在顶层添加一个“类型”字段?

有关详细信息,请参阅https://eagain.net/articles/go-dynamic-json/上的“如何将所有内容放在顶层”部分。

例如(未经测试):

func UnmarshalJSON(d []byte) error {

    var jsonValue map[string]interface{}

    err := json.Unmarshal(d, &jsonValue)


    if err != nil {

        return err

    }


    switch jsonValue["type"] {

    case 1:

        // unmarshal into struct type 1

    case 2:

        // unmarshal into struct type 2

    default:

        // throw err

    }


    // or if you don't have access to type:

    if jsonValue["connectionID"] != nil {

        // unmarshal into struct type 1

    }


    return nil

}

或者,您可以尝试(严格)解组到每个结构中,直到您没有收到错误,例如:


func DetermineStruct(d []byte) int {

    var connStatus *ConnStatus


    reader := bytes.NewReader(d)

    decoder := json.NewDecoder(reader)

    decoder.DisallowUnknownFields()


    err := decoder.Decode(connStatus)

    if err == nil {

        panic(err)

    }


    err = json.Unmarshal(d, &connStatus)

    if err == nil {

        return 1

    }


    var ohlcSuccess OHLCsuccess

    err = json.Unmarshal(d, &ohlcSuccess)

    if err == nil {

        return 2

    }

}


查看完整回答
反对 回复 2022-11-28
  • 1 回答
  • 0 关注
  • 147 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号