我有一个关于在 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 块中,如果我重新解码整个包,我可以让它工作,但我认为这是一种浪费,因为我已经部分解码了它并且只需要解码地图的值,这是一个interface{},但我似乎无法让它正常工作。


但是,如果我执行以下操作,其中 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)

    }

因此,如果无需再次对整个内容重新解码/解组,您如何使用地图中的接口{}?