Skip to content

Instantly share code, notes, and snippets.

@madflojo
Created November 27, 2020 00:37
Show Gist options
  • Save madflojo/645facb5519d91f3f59f76c8c00354cd to your computer and use it in GitHub Desktop.
Save madflojo/645facb5519d91f3f59f76c8c00354cd to your computer and use it in GitHub Desktop.
maps.vs.structs.structparser
package main
import (
"encoding/json"
"fmt"
)
// Example is our main data structure used for JSON parsing
type Example struct {
Name string `json:"name"`
Numbers []int `json:"numbers"`
Nested Nested `json:"nested"`
}
// Nested is an embedded structure within Example
type Nested struct {
IsIt bool `json:"isit"`
Description string `json:"description"`
}
func main() {
// Define a JSON string
j := `{"name":"example","numbers":[1,2,3,4],"nested":{"isit":true,"description":"a nested json"}}`
// Parse JSON string into data object
var data Example
err := json.Unmarshal([]byte(j), &data)
if err != nil {
fmt.Printf("Error parsing JSON string - %s", err)
}
// Print the name
fmt.Printf("Name is %s", data.Name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment