json-interface

command
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 3 Imported by: 0

README

JSON

Decoding

Input
{
  "router": [
    {
      "hostname": "router1.example.com",
      "ip": "192.0.2.1",
      "asn": 64512
    },
    {
      "hostname": "router2.example.com",
      "ip": "198.51.100.1",
      "asn": 65535
    }
  ]
}
Code

Decode reads the next JSON-encoded value from its input and stores it in the empty interface

func main() {
	file, err := os.Open("input.json")
	if err != nil {
		panic(err)
	}
	defer file.Close()
	d := json.NewDecoder(file)

	var empty map[string]interface{}

	err = d.Decode(&empty)
	if err != nil {
		panic(err)
	}

	for _, r := range empty["router"].([]interface{}) {
		fmt.Printf("%v\n", r)
	}

}

Output
⇨  go run main.go 
map[asn:64512 hostname:router1.example.com ip:192.0.2.1]
map[asn:65535 hostname:router2.example.com ip:198.51.100.1]

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL