1. import (
    2. "encoding/json"
    3. "fmt"
    4. )
    5. type Student struct {
    6. ID int
    7. Gender string
    8. Name string //私有属性不能被json包访问
    9. Sno string
    10. }
    11. func main() {
    12. var s1 = Student{
    13. ID: 12,
    14. Gender: "男",
    15. Name: "李四",
    16. Sno: "s0001",
    17. }
    18. fmt.Printf("%#v\n", s1) //main.Student{ID:12, Gender:"男", Name:"李四", Sno:"s0001"}
    19. jsonByte, _ := json.Marshal(s1)
    20. jsonStr := string(jsonByte)
    21. fmt.Printf("%v", jsonStr) //{"ID":12,"Gender":"男","Name":"李四","Sno":"s0001"}
    22. }
    1. package main
    2. import (
    3. "encoding/json"
    4. "fmt"
    5. )
    6. type Student struct {
    7. ID int
    8. Gender string
    9. Name string //私有属性不能被json包访问
    10. Sno string
    11. }
    12. func main() {
    13. //json字符串
    14. var str = `{"ID":12,"Gender":"男","Name":"李四","Sno":"s0001"}`
    15. var s1 Student
    16. err := json.Unmarshal([]byte(str), &s1)
    17. if err != nil {
    18. fmt.Println(err)
    19. }
    20. fmt.Printf("%#v\n", s1)
    21. fmt.Println(s1.Name)
    22. }
    1. package main
    2. import (
    3. "encoding/json"
    4. "fmt"
    5. )
    6. //结构体标签
    7. type Student struct {
    8. Id int `json:"id"`
    9. Gender string `json:"gender"`
    10. Name string `json:"name"` //私有属性不能被json包访问
    11. Sno string `json:"sno"`
    12. }
    13. func main() {
    14. var s1 = Student{
    15. Id: 12,
    16. Gender: "男",
    17. Name: "李四",
    18. Sno: "s0001",
    19. }
    20. fmt.Printf("%#v\n", s1) //main.Student{Id:12, Gender:"男", Name:"李四", Sno:"s0001"}
    21. jsonByte, _ := json.Marshal(s1)
    22. jsonStr := string(jsonByte)
    23. fmt.Printf("%v", jsonStr) //{"id":12,"gender":"男","name":"李四","sno":"s0001"}
    24. }
    1. package main
    2. import (
    3. "encoding/json"
    4. "fmt"
    5. )
    6. //Student 学生
    7. type Student struct {
    8. Id int
    9. Gender string
    10. Name string
    11. }
    12. //Class 班级
    13. type Class struct {
    14. Title string
    15. Students []Student
    16. }
    17. func main() {
    18. c := Class{
    19. Title: "001班",
    20. Students: make([]Student, 0),
    21. }
    22. for i := 1; i <= 10; i++ {
    23. s := Student{
    24. Id: i,
    25. Gender: "男",
    26. Name: fmt.Sprintf("stu_%v", i),
    27. }
    28. c.Students = append(c.Students, s)
    29. }
    30. // fmt.Println(c)
    31. strByte, err := json.Marshal(c)
    32. if err != nil {
    33. fmt.Println(err)
    34. } else {
    35. strJson := string(strByte)
    36. fmt.Println(strJson)
    37. }
    38. }
    1. package main
    2. import (
    3. "encoding/json"
    4. "fmt"
    5. )
    6. //Student 学生
    7. type Student struct {
    8. ID int
    9. Gender string
    10. Name string
    11. }
    12. //Class 班级
    13. type Class struct {
    14. Title string
    15. Students []Student
    16. }
    17. func main() {
    18. str := `{"Title":"001班","Students":[{"Id":1,"Gender":"男","Name":"stu_1"},{"Id":2,"Gender":"男","Name":"stu_2"},{"Id":3,"Gender":"男","Name":"stu_3"},{"Id":4,"Gender":"男","Name":"stu_4"},{"Id":5,"Gender":"男","Name":"stu_5"},{"Id":6,"Gender":"男","Name":"stu_6"},{"Id":7,"Gender":"男","Name":"stu_7"},{"Id":8,"Gender":"男","Name":"stu_8"},{"Id":9,"Gender":"男","Name":"stu_9"},{"Id":10,"Gender":"男","Name":"stu_10"}]}`
    19. var c = &Class{}
    20. err := json.Unmarshal([]byte(str), c)
    21. if err != nil {
    22. fmt.Println(err)
    23. } else {
    24. fmt.Printf("%#v\n", c)
    25. fmt.Printf("%v", c.Title)
    26. }
    27. }