Created
September 24, 2012 21:26
-
-
Save cflewis/3778483 to your computer and use it in GitHub Desktop.
Parse JSON example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
) | |
type Airport struct { | |
Code string `json:"code"` | |
Name string `json:"name"` | |
Location string `json:"string"` | |
} | |
func main() { | |
response, err := http.Get("https://raw.github.com/jbrooksuk/JSON-Airports/master/airports.json") | |
if err != nil { | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
defer response.Body.Close() | |
responseBody, err := ioutil.ReadAll(response.Body) | |
var airports []Airport | |
err = json.Unmarshal(responseBody, &airports) | |
if err != nil { | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
fmt.Printf("%+v", airports) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment