This commit is contained in:
Alexander Pivkin 2025-10-20 13:50:53 +03:00
parent cc6c71ce15
commit 20a62ace05
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,3 @@
module main
go 1.25.1

View File

@ -0,0 +1,67 @@
package main
import (
"encoding/json"
"fmt"
"log"
)
type Message struct {
Key string `json:"id"`
Val string `json:"user"`
}
type Receiver struct {
First string `json:"id"`
Second string `json:"user"`
}
type Test struct {
Name string
Age float64
Parents []string
}
func main() {
m := Message{"lol","kek"}
b,err := json.Marshal(m)
if err != nil {
log.Fatalln("Couldn marshal")
}
fmt.Println("Binary format on marshalled JSON is ",b)
fmt.Println("This is how lookg in string ",string(b))
var k Receiver
err = json.Unmarshal(b,&k)
if err != nil {
log.Fatalln("Couldn UNmarshal")
}
// fmt.Println("This is how looks unmarshalled JSON ", k)
b = []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`)
var f interface{}
_ = json.Unmarshal(b,&f)
fmt.Println(f)
l := f.(map[string]interface{})
for k, v := range l {
switch vv := v.(type) {
case string:
fmt.Println(k, "is string", vv)
case float64:
fmt.Println(k, "is float64", vv)
case []interface{}:
fmt.Println(k, "is an array:")
for i, u := range vv {
fmt.Println(i, u)
}
default:
fmt.Println(k, "is of a type I don't know how to handle")
}
}
var e Test
_ = json.Unmarshal(b,&e)
fmt.Println(e)
}