aprendago icon indicating copy to clipboard operation
aprendago copied to clipboard

Exercício: Capítulo 17, Exercício 2 (Nível: 8)

Open vkorbes opened this issue 5 years ago • 8 comments

Exercício: Capítulo 17, Exercício 2 (Nível: 8)

Link para o vídeo:

Use esta thread para compartilhar sua solução, discutir o exercício com os colegas e pedir ajuda caso tenha dificuldades!

vkorbes avatar Sep 30 '20 17:09 vkorbes

Esse deu um trabalhinho legal, tive que lembrar que tinha que criar um slice vazio do tipo do struct e passar o ponteiro como referencia pro Unmarshal.

https://play.golang.org/p/cgx1RqLqphI

diegoparra avatar Oct 16 '20 00:10 diegoparra

https://play.golang.org/p/qSUTgHHnkhF

Adaptar isso foi facil, inicialmente precisamos converter json-to-go e tomando como base um exemplo Unmarshal foi rapido fazer

package main

import (
	"encoding/json"
	"fmt"
)

/* - Partindo do código abaixo, utilize unmarshal e demonstre os valores.
    - https://play.golang.org/p/b_UuCcZag9​
- Dica: JSON-to-Go.
*/

type MyJsonName struct {
	Age     int64    `json:"Age"`
	First   string   `json:"First"`
	Last    string   `json:"Last"`
	Sayings []string `json:"Sayings"`
}

func main() {
	data := []byte(`[{"First":"James","Last":"Bond","Age":32,"Sayings":["Shaken, not stirred","Youth is no guarantee of innovation","In his majesty's royal service"]},{"First":"Miss","Last":"Moneypenny","Age":27,"Sayings":["James, it is soo good to see you","Would you like me to take care of that for you, James?","I would really prefer to be a secret agent myself."]},{"First":"M","Last":"Hmmmm","Age":54,"Sayings":["Oh, James. You didn't.","Dear God, what has James done now?","Can someone please tell me where James Bond is?"]}]`)

	//func Unmarshal(data []byte, v interface{}) error
	var lista []MyJsonName
	err := json.Unmarshal(data, &lista)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(lista)
}

Output

[{32 James Bond [Shaken, not stirred Youth is no guarantee of innovation In his majesty's royal service]} {27 Miss Moneypenny [James, it is soo good to see you Would you like me to take care of that for you, James? I would really prefer to be a secret agent myself.]} {54 M Hmmmm [Oh, James. You didn't. Dear God, what has James done now? Can someone please tell me where James Bond is?]}]

Program exited.

an4kein avatar Mar 02 '21 18:03 an4kein

Usei o for para percorrer o blob de users e mostrar o resultado na tela.

Essa foi minha solução:

package main

import (
	"encoding/json"
	"fmt"
)

type User struct {
	First   string   `json:"first"`
	Last    string   `json:"last"`
	Age     int      `json:"age"`
	Sayings []string `json:"sayings"`
}

func main() {
	blob := `[{"First":"James","Last":"Bond","Age":32,"Sayings":["Shaken, not stirred","Youth is no guarantee of innovation","In his majesty's royal service"]},{"First":"Miss","Last":"Moneypenny","Age":27,"Sayings":["James, it is soo good to see you","Would you like me to take care of that for you, James?","I would really prefer to be a secret agent myself."]},{"First":"M","Last":"Hmmmm","Age":54,"Sayings":["Oh, James. You didn't.","Dear God, what has James done now?","Can someone please tell me where James Bond is?"]}]`
	var users []User

	err := json.Unmarshal([]byte(blob), &users)
	if err != nil {
		fmt.Println(err)
	}

	for _, user := range users {
		fmt.Println("Olá meu nome é", user.First, user.Last)
		fmt.Println("Tenho", user.Age, "anos")
		fmt.Println("Sayings")
		for _, v := range user.Sayings {
			fmt.Println("-", v)
		}
	}

}

Go Playground: https://play.golang.org/p/rslxHnGFVO2

ghost avatar Aug 25 '21 22:08 ghost

Cap. 17 – Exercícios: Nível #8 – 2 https://go.dev/play/p/pk8CO-z637b

image

wfrsilva avatar Jun 01 '22 00:06 wfrsilva

https://go.dev/play/p/4uCTS446Unz

Harsgaard avatar Dec 18 '22 05:12 Harsgaard

https://go.dev/play/p/Kh476CfD-92

package main

import ( "encoding/json" "fmt" )

type Toneio []struct { First string json:"First" Age int json:"Age" }

func main() { tr := []byte([{"First":"James","Age":32},{"First":"Moneypenny","Age":27},{"First":"M","Age":54}]) var coisa Toneio er := json.Unmarshal(tr, &coisa) if er != nil { fmt.Println(er)

}
fmt.Println(coisa)

}

LelecoNN avatar Oct 09 '23 03:10 LelecoNN