在 Golang 中解码任意 JSON
Decode arbitrary JSON in Golang
我有一个关于在 Go 中解码任意 JSON objects/messages 的问题。假设你有三个截然不同的 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
更容易。有关非常详细的讨论,请参阅 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,那么解组应该只填充相关的。你可以玩玩它 here.
我有一个关于在 Go 中解码任意 JSON objects/messages 的问题。假设你有三个截然不同的 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
更容易。有关非常详细的讨论,请参阅 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,那么解组应该只填充相关的。你可以玩玩它 here.