Deserializing basics

Report a typo

Suppose you have two of the most simple JSON objects: carsJSON, which is a map, and dogsJSON, which is a slice.

Write the required code to properly deserialize carsJSON and dogsJSON to the cars and dogs variables and print their deserialized values.

This problem uses the bufio package to scan the whitespace-separated strings from the standard input. However, to solve this task, you don't need to know how the bufio package works. The only objective is to call the correct function from the encoding/json package to deserialize carsJSON and dogsJSON to cars and dogs, respectively, and then print their deserialized values.

Sample Input 1:

{"brand": "Tesla", "model": "Model S", "cost": 94990.00}
["Maltese", "Boxer", "Yorkshire Terrier"]

Sample Output 1:

map[brand:Tesla cost:94990 model:Model S]
[Maltese Boxer Yorkshire Terrier]
Write a program in Go
package main

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

func main() {
// DO NOT delete! - This code block takes as an input the values for the
// 'carsJSON' and 'dogsJSON' variables:
scanner := bufio.NewScanner(os.Stdin)

scanner.Scan()
carsJSON := scanner.Text()

scanner.Scan()
dogsJSON := scanner.Text()

// Deserialize carsJSON below:
var cars map[string]interface{}
err := json.Unmarshal([]byte(?), &?)
if err != nil {
log.Fatal(err)
}
fmt.Println(cars)

// Deserialize dogsJSON below:
var dogs []string
err = json.?([]byte(?), &?)
if err != nil {
log.Fatal(err)
}
fmt.Println(dogs)
___

Create a free account to access the full topic