I am attempting to read a json file that looks like
[{ "title": "hi", "tags": [1,2,3,4,5,6] }, {...}, {...}]
and the code looks like this
contentdat, err := ioutil.ReadFile("content.json")
check(err)
var content []interface{}
err = json.Unmarshal(contentdat, &content)
check(err)
for i, contentItem := range content {
vertedContentItem := contentItem.(map[string]interface{})
contentTags := vertedContentItem["tags"].([]interface{})
contentItemTags := make([]int, len(contentTags))
for i, ctv := range contentTags {
contentItemTags[i] = int(ctv.(float64))
}
what I am trying to figure out is how I can avoid the doing all the type casting and just access the json obj directly or maybe just type cast once for the entire json structure. I had a idea about defining the structure of the internal object like so
type Content struct {
title string
tags []int
}
and then declare content as
var content []Content
instead of interface{} and just loop through the structure as expected but that didn't work. Any ideas?