Created
October 27, 2022 09:00
-
-
Save ismdeep/451312cd5810f8d65e17795b47bdfec2 to your computer and use it in GitHub Desktop.
extract interface{} from json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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