package main
import (
"fmt"
"encoding/json"
)
type People struct {
Name string `json:"name_title"`
Age int `json:"age_size"`
}
func JsonToStructDemo(){
jsonStr := `
{
"name_title": "jqw"
"age_size":12
}
`
var people People
json.Unmarshal([]byte(jsonStr), &people)
fmt.Println(people)
}
func main(){
JsonToStructDemo()
}
注意 json 里面的 key 和 struct 里面的 key 要一致, struct 中的 key 的首字母必须大写, 而 json 中大小写都可以。
在结构体中引入 tag 标签, 这样匹配的时候 json 串对应的字段名需要与 tag 标签中定义的字段名匹配, 当然 tag 中定义的名称不需要首字母大写, 且对应的 json 串中字段名仍然大小写不敏感。此时, 结构体中对应的字段名可以不用和匹配的一致, 但是首字母必须大写, 只有大写才是可对外提供访问的。
package main
import (
"fmt"
"encoding/json"
)
type People struct {
Name string `json:"name_title"`
Age int `json:"age_size"`
}
func StructToJsonDemo(){
p := People{
Name: "jqw",
Age: 18,
}
jsonBytes, err := json.Marshal(p)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(jsonBytes))
}
func main(){
StructToJsonDemo()
}
func JsonToMapDemo(){
jsonStr := `
{
"name": "jqw",
"age": 18
}
`
var mapResult map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &mapResult)
if err != nil {
fmt.Println("JsonToMapDemo err:", err)
}
fmt.Println(mapResult)
}
func MapToJsonDemo1(){
mapInstances := []map[string]interface{}{}
instance_1 := map[string]interface{}{"name": "John", "age": 10}
instance_2 := map[string]interface{}{"name": "Alex", "age": 12}
mapInstances = append(mapInstances, instance_1, instance_2)
jsonStr, err := json.Marshal(mapInstances)
if err != nil {
fmt.Println("MapToJsonDemo err:", err)
}
fmt.Println(string(jsonStr))
}
func MapToJsonDemo2(){
b, _ := json.Marshal(map[string]int{"test":1, "try":2})
fmt.Println(string(b))
}
go get github.com/goinggo/mapstructure
func MapToStructDemo(){
mapInstance := make(map[string]interface{})
mapInstance["Name"] = "jqw"
mapInstance["Age"] = 18
var people People
err := mapstructure.Decode(mapInstance, &people)
if err != nil {
fmt.Println(err)
}
fmt.Println(people)
}
func StructToMapDemo(obj interface{}) map[string]interface{}{
obj1 := reflect.TypeOf(obj)
obj2 := reflect.ValueOf(obj)
var data = make(map[string]interface{})
for i := 0; i < obj1.NumField(); i++ {
data[obj1.Field(i).Name] = obj2.Field(i).Interface()
}
return data
}
func TestStructToMap(){
student := Student{10, "jqw", 18}
data := StructToMapDemo(student)
fmt.Println(data)
}
Go has inside the standard library all things needed to marshal/unmarshal JSON (Marshaling is the process of transforming an object into a storable representation using a specific format).
To pretty-print JSON, you can use json.MarshalIndent
:
package main
import (
"encoding/json"
"fmt"
"log"
)
type Teacher struct {
ID string `json:"id"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
func main() {
john := Teacher{
ID: "678930",
Firstname: "John",
Lastname: "Doe",
}
marshaled, err := json.MarshalIndent(john, "", " ")
if err != nil {
log.Fatalf("marshaling error: %s", err)
}
fmt.Println(string(marshaled))
}
Parameters of json.MarshalIndent
:
This program outputs :
{
"id": "678930",
"firstname": "John",
"lastname": "Doe"
}