结构体 生成 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}
// 根据内容生成json
buf, err := json.Marshal(it)
if err != nil {
fmt.Println("err=", err)
return
}
fmt.Println("buf = ", string(buf))
/**
正常转换,json key也是大写
buf = {"Company":"hello","Subjects":["Vue","React","Angular"],"IsOk":true,"Price":66.66}
*/
}
/*
结构体字段首字母大写, 有标签
/
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}
// 根据内容生成json
buf, err := json.Marshal(it3)
if err != nil {
fmt.Println("err=", err)
return
}
fmt.Println("buf = ", string(buf))
/**
正常转换,json key 小写
buf = {"company":"hello","subjects":["Vue","React","Angular"],"isOk":true}
*/
}
/* 结构体字段名称首字母小写 / type IT2 struct { Company string isOk bool price float64 }
func init() { // 初始化结构体变量it it2 := IT2{“hello”, true, 66.66}
// 根据内容生成json
buf, err := json.Marshal(it2)
if err != nil {
fmt.Println("err=", err)
return
}
fmt.Println("buf = ", string(buf))
/**
只转了大写的
buf = {"Company":"hello"}
*/
}
func main() {
/*
buf = {"Company":"hello","Subjects":["Vue","React","Angular"],"IsOk":true,"Price":66.66}
buf = {"Company":"hello"}
buf = {"company":"hello","subjects":["Vue","React","Angular"],"isOk":true}
*/
}
- 如果变量首字母小写,则为private。无论如何不能转,因为取不到反射信息。
- 如果变量首字母大写,则为public。
- 不加tag,可以正常转为json里的字段,json内字段名跟结构体内字段原名一致。
- 加了tag,从struct转json的时候,json的字段名就是tag里的字段名,原字段名已经没用。
<a name="eXCF8"></a>
# map 生成 json
```go
package main
import (
"encoding/json"
"fmt"
)
func main() {
/**
mapName := map[keyType]valueType
*/
bookInfo := make(map[string]interface{}, 4)
bookInfo["company"] ="hello"
bookInfo["subjects"] = []string{"Go", "C++", "C"}
bookInfo["isOk"] = true
bookInfo["price"] = 666.666
result, err := json.MarshalIndent(bookInfo, "", " ")
if err != nil {
fmt.Println("err = ", err)
return
}
fmt.Println("result = ", string(result))
/**
result = {
"company": "hello",
"isOk": true,
"price": 666.666,
"subjects": [
"Go",
"C++",
"C"
]
}
*/
}
json 生成 结构体
package main
import (
"encoding/json"
"fmt"
)
func init() {
// 有无tag 都会成功
type Front struct {
Company string `json:"company"`
Subjects []string `json:"subjects"`
IsOk bool `json:"isOk"`
Price float64 `json:"price"`
}
frontBuf := `{
"company": "hello",
"subjects": ["Vue","React","Angular"],
"isOk": true,
"price": 66.66
}`
var front Front
err := json.Unmarshal([]byte(frontBuf), &front)
if (err != nil) {
fmt.Println("err = ", err)
return
}
fmt.Printf("front = %+v\n", front)
fmt.Println("front = ", front)
fmt.Println("front.Subjects:: ", front.Subjects)
/*
front = {Company:hello Subjects:[Vue React Angular] IsOk:true Price:66.66}
front = {hello [Vue React Angular] true 66.66}
front.Subjects:: [Vue React Angular]
*/
}
func main() {
}
json 生成 map
package main
import (
"encoding/json"
"fmt"
)
func init() {
frontBuf := `{
"company": "hello",
"subjects": ["Vue","React","Angular"],
"isOk": true,
"price": 66.66
}`
list := make(map[string]interface{}, 4)
err := json.Unmarshal([]byte(frontBuf), &list)
if (err != nil) {
fmt.Println("err = ", err)
return
}
fmt.Printf("front = %+v\n", list)
fmt.Println("front = ", list)
/*
front = map[company:hello isOk:true price:66.66 subjects:[Vue React Angular]]
front = map[company:hello isOk:true price:66.66 subjects:[Vue React Angular]]
*/
for key, value := range list {
switch data := value.(type) {
case string:
print(key, value, data)
case bool:
print(key, value, data)
case float64:
print(key, value, data)
case []string:
print(key, value, data)
case []interface{}:
print(key, value, data)
}
}
/*
list[company] type is string value: hello
list[subjects] type is []interface {} value: [Vue React Angular]
list[isOk] type is bool value: true
list[price] type is float64 value: 66.66
*/
}
func print(key interface{}, value interface{}, data interface{}) {
fmt.Printf("list[%s] type is %T", key, value)
fmt.Printf(" value: %v \n", value)
}
func main() {
}