You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know it's an old thread, but for anyone wondering what to do... You could simply marshal the interface{} back into a JSON string an then unmarshal into a struct. It may not be the fastest solution, but if you know upfront what the JSON structure is then it may be an interesting alternative:
package main
import (
"encoding/json""log"
)
funcmain() {
// Using the blob from the json.Unmarshal documentation example...varjsonBlob= []byte(`[ {"Name": "Platypus", "Order": "Monotremata"}, {"Name": "Quoll", "Order": "Dasyuromorphia"}]`)
// Unmarshal into empty interfacevareventinterface{}
err:=json.Unmarshal(jsonBlob, &event)
iferr!=nil {
log.Fatal("Unable to unmarshal:", err)
}
log.Println("Empty interface:", event) // <-- this is what you get from go-lumber// Marshal back into JSON stringjsonString, err:=json.Marshal(event)
iferr!=nil {
log.Fatal("Unable to marshal:", err)
}
log.Println("JSON string:", string(jsonString))
// Unmarshal into structtypeAnimalstruct {
NamestringOrderstring
}
varanimals []Animaljson.Unmarshal(jsonString, &animals)
log.Println("Animals:", animals)
}
Hand casting each key of a complex map[string]interface{} is very painful.
The text was updated successfully, but these errors were encountered: