Emma has created a Go program that takes as an input a string with a JSON object and saves it to the cerealsJSON variable:
{
"cereals": [
{
"name": "100% Bran",
"nutritionFacts": {
"calories": 70,
"protein": 4,
"fat": 1,
"carbo": 5
},
"rating": 68.402973
},
{
"name": "100% Natural Bran",
"nutritionFacts": {
"calories": 120,
"protein": 3,
"fat": 5,
"carbo": 8
},
"rating": 33.983679
}
]
}
Now she wants to decode the above JSON object into a struct, to do this she needs to perform the following steps:
- Create the
CerealJSONObjectstruct that can adequately hold the values and follow the structure of the above JSON object - Write the required code to call the
json.Unmarshal()function and use it to decode the JSON object stored withincerealsJSONinto thecerealsObjvariable of theCerealsJSONObjecttype. - Print the decoded
cerealsObjstruct using the"%+v"verb.
This problem uses the
bufio package to scan the whitespace-separated strings from the standard input and save the above JSON object into the cerealsJSON variable. However, don't be scared! you don't need to know how the bufio package works to solve this task.Tip: If you are stuck trying to figure out the proper struct to use, you can paste the above JSON object into the JSON-to-Go struct converter!