Created
October 23, 2019 10:11
-
-
Save bechampion/26f1e5df11e46681f9df33a628fc377a to your computer and use it in GitHub Desktop.
cast golang
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" | |
"reflect" | |
) | |
type FruitBasket struct { | |
Name string | |
Fruit interface{} | |
} | |
func main() { | |
jsonData := []byte(` | |
{ | |
"Name": "Standard", | |
"Fruit": [ | |
"Apple", | |
"Banana", | |
"Orange" | |
], | |
"ref": 999, | |
"Created": "2018-04-09T23:00:00Z" | |
}`) | |
var basket FruitBasket | |
json.Unmarshal(jsonData, &basket) | |
fmt.Println(basket.Name) | |
fmt.Println(basket.Fruit) | |
fmt.Println(reflect.TypeOf(basket.Fruit)) | |
for _,i := range basket.Fruit.([]interface{}) { | |
fmt.Println(reflect.TypeOf(i)) | |
fmt.Println(i) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment