猿问

在 Golang 中解码任意 JSON

我有一个关于在 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)

    }

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


忽然笑
浏览 253回答 1
1回答

杨魅力

如果您有类似 map[string]interface{} 的东西,那么您可以使用类型断言访问这些值,例如home, valid := msg["home"].(string)if !valid {    return}这适用于简单的值。对于更复杂的嵌套结构,您可能会发现json.RawMessage使用自定义json.Unmarshaler. 有关非常详细的讨论,请参阅this。另一个想法可能是定义一个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"`}如果您将这些设置为省略 if nil,则解组应该只填充相关的。你可以在这里玩它。
随时随地看视频慕课网APP

相关分类

Go
我要回答