结构体 生成 json

  • 结构体字段首字母大写, 无标签 (正常转)
  • 结构体字段首字母大写, 有标签 (正常转)
  • 结构体字段首字母小写 (失败) ```go package main

import ( “encoding/json” “fmt” )

/* 结构体字段首字母大写, 无标签 / type IT struct { Company string Subjects []string IsOk bool Price float64 }

func init() { // 初始化结构体变量it it := IT{“hello”, []string{“Vue”, “React”, “Angular”}, true, 66.66}

  1. // 根据内容生成json
  2. buf, err := json.Marshal(it)
  3. if err != nil {
  4. fmt.Println("err=", err)
  5. return
  6. }
  7. fmt.Println("buf = ", string(buf))
  8. /**
  9. 正常转换,json key也是大写
  10. buf = {"Company":"hello","Subjects":["Vue","React","Angular"],"IsOk":true,"Price":66.66}
  11. */

}

/* 结构体字段首字母大写, 有标签 / type IT3 struct { Company string json:"company" Subjects []string json:"subjects" IsOk bool json:"isOk" Price float64 json:"price" }

func init() { // 初始化结构体变量it it3 := IT3{“hello”, []string{“Vue”, “React”, “Angular”}, true, 66.66}

  1. // 根据内容生成json
  2. buf, err := json.Marshal(it3)
  3. if err != nil {
  4. fmt.Println("err=", err)
  5. return
  6. }
  7. fmt.Println("buf = ", string(buf))
  8. /**
  9. 正常转换,json key 小写
  10. buf = {"company":"hello","subjects":["Vue","React","Angular"],"isOk":true}
  11. */

}

/* 结构体字段名称首字母小写 / type IT2 struct { Company string isOk bool price float64 }

func init() { // 初始化结构体变量it it2 := IT2{“hello”, true, 66.66}

  1. // 根据内容生成json
  2. buf, err := json.Marshal(it2)
  3. if err != nil {
  4. fmt.Println("err=", err)
  5. return
  6. }
  7. fmt.Println("buf = ", string(buf))
  8. /**
  9. 只转了大写的
  10. buf = {"Company":"hello"}
  11. */

}

func main() {

  1. /*
  2. buf = {"Company":"hello","Subjects":["Vue","React","Angular"],"IsOk":true,"Price":66.66}
  3. buf = {"Company":"hello"}
  4. buf = {"company":"hello","subjects":["Vue","React","Angular"],"isOk":true}
  5. */

}

  1. - 如果变量首字母小写,则为private。无论如何不能转,因为取不到反射信息。
  2. - 如果变量首字母大写,则为public
  3. - 不加tag,可以正常转为json里的字段,json内字段名跟结构体内字段原名一致。
  4. - 加了tag,从structjson的时候,json的字段名就是tag里的字段名,原字段名已经没用。
  5. <a name="eXCF8"></a>
  6. # map 生成 json
  7. ```go
  8. package main
  9. import (
  10. "encoding/json"
  11. "fmt"
  12. )
  13. func main() {
  14. /**
  15. mapName := map[keyType]valueType
  16. */
  17. bookInfo := make(map[string]interface{}, 4)
  18. bookInfo["company"] ="hello"
  19. bookInfo["subjects"] = []string{"Go", "C++", "C"}
  20. bookInfo["isOk"] = true
  21. bookInfo["price"] = 666.666
  22. result, err := json.MarshalIndent(bookInfo, "", " ")
  23. if err != nil {
  24. fmt.Println("err = ", err)
  25. return
  26. }
  27. fmt.Println("result = ", string(result))
  28. /**
  29. result = {
  30. "company": "hello",
  31. "isOk": true,
  32. "price": 666.666,
  33. "subjects": [
  34. "Go",
  35. "C++",
  36. "C"
  37. ]
  38. }
  39. */
  40. }

json 生成 结构体

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. func init() {
  7. // 有无tag 都会成功
  8. type Front struct {
  9. Company string `json:"company"`
  10. Subjects []string `json:"subjects"`
  11. IsOk bool `json:"isOk"`
  12. Price float64 `json:"price"`
  13. }
  14. frontBuf := `{
  15. "company": "hello",
  16. "subjects": ["Vue","React","Angular"],
  17. "isOk": true,
  18. "price": 66.66
  19. }`
  20. var front Front
  21. err := json.Unmarshal([]byte(frontBuf), &front)
  22. if (err != nil) {
  23. fmt.Println("err = ", err)
  24. return
  25. }
  26. fmt.Printf("front = %+v\n", front)
  27. fmt.Println("front = ", front)
  28. fmt.Println("front.Subjects:: ", front.Subjects)
  29. /*
  30. front = {Company:hello Subjects:[Vue React Angular] IsOk:true Price:66.66}
  31. front = {hello [Vue React Angular] true 66.66}
  32. front.Subjects:: [Vue React Angular]
  33. */
  34. }
  35. func main() {
  36. }

json 生成 map

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. func init() {
  7. frontBuf := `{
  8. "company": "hello",
  9. "subjects": ["Vue","React","Angular"],
  10. "isOk": true,
  11. "price": 66.66
  12. }`
  13. list := make(map[string]interface{}, 4)
  14. err := json.Unmarshal([]byte(frontBuf), &list)
  15. if (err != nil) {
  16. fmt.Println("err = ", err)
  17. return
  18. }
  19. fmt.Printf("front = %+v\n", list)
  20. fmt.Println("front = ", list)
  21. /*
  22. front = map[company:hello isOk:true price:66.66 subjects:[Vue React Angular]]
  23. front = map[company:hello isOk:true price:66.66 subjects:[Vue React Angular]]
  24. */
  25. for key, value := range list {
  26. switch data := value.(type) {
  27. case string:
  28. print(key, value, data)
  29. case bool:
  30. print(key, value, data)
  31. case float64:
  32. print(key, value, data)
  33. case []string:
  34. print(key, value, data)
  35. case []interface{}:
  36. print(key, value, data)
  37. }
  38. }
  39. /*
  40. list[company] type is string value: hello
  41. list[subjects] type is []interface {} value: [Vue React Angular]
  42. list[isOk] type is bool value: true
  43. list[price] type is float64 value: 66.66
  44. */
  45. }
  46. func print(key interface{}, value interface{}, data interface{}) {
  47. fmt.Printf("list[%s] type is %T", key, value)
  48. fmt.Printf(" value: %v \n", value)
  49. }
  50. func main() {
  51. }