在Golang中解码任意JSON

约旦2175

我有一个关于在Go中解码任意JSON对象/消息的问题。让我们说,例如,您可以在http连接上收到三个截然不同的JSON对象(又称消息),为了说明起见,我们称它们为:

{ home : { some unique set of arrays, objects, fields, and arrays objects } }

{ bike : { some unique set of arrays, objects, fields, and arrays objects } }

{ soda : { some unique set of arrays, objects, fields, and arrays objects } }

我在想的是,您可以将它们从http连接解码为接口映射,例如:

func httpServerHandler(w http.ResponseWriter, r *http.Request) {
    message := make(map[string]interface{})
    decoder := json.NewDecoder(r.Body)
    _ = decoder.Decode(&message)

然后执行if,else if块以查找有效的JSON消息

if _, ok := message["home"]; ok {
    // Decode interface{} to appropriate struct
} else if _, ok := message["bike"]; ok {
    // Decode interface{} to appropriate struct
} else {
    // Decode interface{} to appropriate struct
}

现在在if块中,如果我重新解码整个包,我可以使其工作,但是我认为这很浪费,因为我已经对其进行了部分解码,并且只需要解码map的值即可。接口{},但似乎无法正常工作。

但是,如果我执行以下类似的操作(例如homeType是一个结构),则可以对整个事情进行重新编码:

var homeObject homeType
var bikeObject bikeType
var sodaObject sodaType

然后在if块中执行:

if _, ok := message["home"]; ok {
    err = json.Unmarshal(r.Body, &homeObject)
    if err != nil {
        fmt.Println("Bad Response, unable to decode JSON message contents")
        os.Exit(1)
    }

因此,在没有重新解码/解组整个事情的情况下,如何处理地图中的interface {}?

阿尔弗雷德·罗西(Alfred Rossi)

如果您有诸如map [string] interface {}之类的东西,则可以使用类型断言来访问值,例如

home, valid := msg["home"].(string)
if !valid {
    return
}

这对于简单的值非常有效。对于更复杂的嵌套结构,您可能会发现更容易json.RawMessage使用自定义延迟解码或实现自定义json.Unmarshaler看到这个非常详细的讨论。

另一个想法可能是定义一个自定义Message类型,类型包括指向Home,Bike和Soda结构的指针。

type Home struct {
    HomeStuff     int
    MoreHomeStuff string
} 

type Bike struct {
    BikeStuff int
}

type Message struct {
    Bike *Bike `json:"Bike,omitempty"`
    Home *Home `json:"Home,omitempty"`
}

如果将这些设置为nil则省略,那么解组应该只填充相关的一个。你可以在这里

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章