繁体   English   中英

在Golang中解码任意JSON

[英]Decode arbitrary JSON in Golang

我有一个关于在Go中解码任意JSON对象/消息的问题。比如说你有三个截然不同的JSON对象(也就是消息)你可以在http连接上接收,为了说明我们可以调用它们:

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

重新编码整个东西,但如果我执行类似以下的操作,例如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)
    }

因此,如果不再重新解码/解组整个事物,您如何使用地图中的界面{}?

如果您有类似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那么解组应该只填充相关的那个。 你可以在这里玩。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM