This commit is contained in:
libin
2023-03-21 08:26:17 +08:00
parent bc3355c2c5
commit 3c506d07c3
8 changed files with 178 additions and 17 deletions

View File

@@ -6,7 +6,7 @@ import (
"testing"
)
func TestJsonToStruct(t *testing.T) {
func TestJsonToStruct1(t *testing.T) {
type Address struct {
City string `json:"city"`
Country string `json:"country"`
@@ -124,3 +124,45 @@ func TestJsonToStruct2(t *testing.T) {
fmt.Printf("person2%+v address%+v \n", person2, person2.Address)
}
// 多层级json测试
func TestJsonToStruct3(t *testing.T) {
type Address struct {
City string `json:"city"`
Street string `json:"street"`
Zipcode uint64 `json:"zipcode"`
}
type Score struct {
Subject string `json:"subject"`
Score int `json:"score"`
}
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
Address Address `json:"address"`
Scores []Score `json:"scores"`
}
jsonStr4 := `{
"name": "Alice",
"age": 30,
"address": {
"city": "Beijing",
"street": "Zhangsan Street",
"zipcode": 100
},
"scores": [
{"subject": "Math", "score": 80},
{"subject": "English", "score": 90}
]
}`
var student Student
if err := JsonToStruct(jsonStr4, &student); err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v \n", student)
}