Answer the question
In order to leave comments, you need to log in
How to convert structure to JSON?
Greetings!
And straight to the point.
package main
import (
"fmt"
"encoding/json"
)
type Department struct {
Id int
Name string
Phone string
}
func main() {
a := Department {1, "test", "12-32"}
fmt.Println(a)
b, _ := json.Marshal(a)
fmt.Println(string(b))
}
{1 test 12-32}
{"Id":1,"Name":"test","Phone":"12-32"}
package main
import (
"fmt"
"encoding/json"
)
type Department struct {
Id int
name string
Phone string
}
func main() {
a := Department {1, "test", "12-32"}
fmt.Println(a)
b, _ := json.Marshal(a)
fmt.Println(string(b))
}
{1 test 12-32}
{"Id":1,"Phone":"12-32"}
Answer the question
In order to leave comments, you need to log in
I will add. In order for json to display fields with a small letter, you need to register a tag.
type Department struct {
Id int `json:"id"`
Name string `json:"name"`
Phone string `json:"phone"`
}
type Department struct {
Id int `json:"id"`
Name string `json:"name"`
Phone string
AnyField string `json:"part"`
}
{id: 123, name: "Имя", Phone: "123", part: "AnyField"}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question