json和类型转换

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?

You must export the fields of the "Content" struct that you wish to de/serialize by using capital letters:

type Content struct {
  Title string
  Tags  []int
}

For example:

type Content struct {
  Title string
  Tags  []int
}

func main() {
  jsonstr := `[{"title":"hi","tags":[1,2,3,4,5,6]}]`
  contents := []Content{}

  err := json.Unmarshal([]byte(jsonstr), &contents)
  if err != nil {
    panic(err)
  }

  fmt.Printf("%#v
", contents)
  // => []main.Content{main.Content{Title:"hi", Tags:[]int{1, 2, 3, 4, 5, 6}}}

}