Deserializing to a struct

Report a typo

Suppose you want to decode to the FootballTeams struct a JSON object that contains data of Bundesliga Football teams:

{
  "league": "Bundesliga",
  "clubs": [
    {
      "name": "Bayern Münich",
      "code": "FCB",
      "stadium": "Allianz Arena"
    },
    {
      "name": "Borussia Dortmund",
      "code": "BVB",
      "stadium": "Signal Iduna Park"
    },
    ... // rest of the teams go here
  ]
}

We have already created the FootballTeams struct for you, your only task is to write the additional code needed to deserialize the above JSON object to the teams struct variable, and then print its deserialized value.

This problem uses the bufio package to scan the whitespace-separated strings from the standard input. However, you don't need to know how the bufio package works to solve this task. The only objective is to call the correct function from the encoding/json package to deserialize teamsJSONand then print its deserialized value.

Sample Input 1:

{"league":"Bundesliga","clubs":[{"name":"Bayern Münich","code":"FCB","stadium":"Allianz Arena"},{"name":"Borussia Dortmund","code":"BVB","stadium":"Signal Iduna Park"}]}

Sample Output 1:

{League:Bundesliga Clubs:[{Name:Bayern Münich Code:FCB Stadium:Allianz Arena} {Name:Borussia Dortmund Code:BVB Stadium:Signal Iduna Park}]}
Write a program in Go
package main

import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
)

// DO NOT delete or modify the FootballTeams struct implementation:
type FootballTeams struct {
League string `json:"league"`
Clubs []struct {
Name string `json:"name"`
Code string `json:"code"`
Stadium string `json:"stadium"`
} `json:"clubs"`
}

func main() {
// DO NOT delete! - This block takes as an input the JSON object for the 'teamsJSON' variable:
scanner := bufio.NewScanner(os.Stdin)

var teamsJSON string
scanner.Scan()
teamsJSON = scanner.Text()

// Write the required code below to properly decode the 'teamsJSON' JSON object to the 'teams' struct:
var teams FootballTeams
err := ?.?([]byte(?), &?)
if err != nil {
log.Fatal(err)
}

// Print the decoded 'teams' struct below -- do not change the "%+v" verb!
fmt.Printf("%+v", ?)
}
___

Create a free account to access the full topic