Skip to content

Instantly share code, notes, and snippets.

@ismdeep
Created October 27, 2022 09:00
Show Gist options
  • Save ismdeep/451312cd5810f8d65e17795b47bdfec2 to your computer and use it in GitHub Desktop.
Save ismdeep/451312cd5810f8d65e17795b47bdfec2 to your computer and use it in GitHub Desktop.
extract interface{} from json
package main
import (
"encoding/json"
"fmt"
"github.com/ismdeep/parser"
)
var contentA = `{"a": "123"}`
var contentB = `{"a": 123}`
type Content struct {
A interface{} `json:"a"`
}
func main() {
var c Content
if err := json.Unmarshal([]byte(contentB), &c); err != nil {
panic(err)
}
var size uint64
switch c.A.(type) {
case float64:
size = uint64(c.A.(float64))
case string:
var err error
size, err = parser.ToUint64(c.A.(string))
if err != nil {
panic(err)
}
default:
panic("test")
}
fmt.Println(size)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment