golang-exercises/parseJSON/main.go

70 lines
1.1 KiB
Go
Raw Permalink Normal View History

package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
)
type people struct {
Example int `json:"number"`
PersonalDetails []struct {
Name string `json:"name"`
Craft string `json:"craft"`
} `json:"people"`
}
func main() {
query := `
{
"message": "success",
"people": [
{
"name": "Jasmin Moghbeli",
"craft": "ISS"
},
{
"name": "Andreas Mogensen",
"craft": "ISS"
},
{
"name": "Satoshi Furukawa",
"craft": "ISS"
},
{
"name": "Konstantin Borisov",
"craft": "ISS"
},
{
"name": "Oleg Kononenko",
"craft": "ISS"
},
{
"name": "Nikolai Chub",
"craft": "ISS"
},
{
"name": "Loral O'Hara",
"craft": "Test"
}
],
"number": 7
}
`
textBytes := []byte(query)
urbDicRequest := people{}
err := json.Unmarshal(textBytes, &urbDicRequest)
if err != nil {
log.Fatalf("Error happened parsing json: %v\n", err)
}
choicString := os.Args[1]
choice, _ := strconv.Atoi(choicString)
fmt.Println(urbDicRequest.PersonalDetails[choice].Name)
fmt.Println(urbDicRequest.PersonalDetails[choice].Craft)
}