Documentation

JSON Go package official documentation

In-Depth explanation of JSON decoding

Code samples/snippets

  1. First case : json file containing an array of 2 json objects

package main

import ( "encoding/json" "fmt" )

func main() {

    `var jsonBlob = []byte(`[`

    `{"Name": "Platypus", "Order": "Monotremata"},`
    `{"Name": "Quoll",    "Order": "Dasyuromorphia"}`
    `]`)`

    `type Animal struct {`
        `Name  string`
        `Order string`
    `}`

    `var animals []Animal`

    `err := json.Unmarshal(jsonBlob, &animals)`

    `if err != nil {`
        `fmt.Println("error:", err)`
    `}`

    `fmt.Printf("%+v", animals)`

}

Index