下面是一段Json格式的数据:
{"name":"lane","age":18,"email":["xxx@qq.com","yyy@qq.com"]}
接下来,我们写一段代码,把Json格式的数据做为字符串输入,我们的代码解析后输出到结构体;然后修改结构体,在以字符串格式输出。
1 将Json string解析为结构体(struct)
package main
import (
"log"
"encoding/json"
)
type Person struct {
Name string
Age int
Email []string
}
var json_str = []byte(`{"name":"lane","age":18,"email":["xxx@qq.com","yyy@qq.com"]}`)
func main() {
p := Person{}
err := json.Unmarshal(json_str, &p)
if err != nil {
log.Print(err)
} else {
log.Print(p)
}
}
2 将结构体(struct)转换为Json string
package main
import (
"log"
"encoding/json"
)
type Person struct {
Name string
Age int
Email []string
}
var json_str = []byte(`{"name":"lane","age":18,"email":["xxx@qq.com","yyy@qq.com"]}`)
func main() {
p := Person{}
err := json.Unmarshal(json_str, &p)
if err != nil {
log.Print(err)
} else {
log.Print(p)
}
p.Age = 19
json_str, err := json.Marshal(p)
if err != nil {
log.Print(err)
} else {
log.Print(string(json_str))
}
}
3 使用Struct Tag
假设我们修改Json string,把age改为how-old,现在Json string是这样的:
{"name":"lane","how-old":18,"email":["xxx@qq.com","yyy@qq.com"]}
我们原来的代码还能正确运行吗?我们试一下:
package main
import (
"log"
"encoding/json"
)
type Person struct {
Name string
Age int
Email []string
}
var json_str = []byte(`{"name":"lane","how-old":18,"email":["xxx@qq.com","yyy@qq.com"]}`)
func main() {
p := Person{}
err := json.Unmarshal(json_str, &p)
if err != nil {
log.Print(err)
} else {
log.Print(p)
}
p.Age = 19
json_str, err := json.Marshal(p)
if err != nil {
log.Print(err)
} else {
log.Print(string(json_str))
}
}
运行结果为:
2021/07/06 12:09:31 {lane 0 [xxx@qq.com yyy@qq.com]}
2021/07/06 12:09:31 {"Name":"lane","Age":19,"Email":["xxx@qq.com","yyy@qq.com"]}
使用Struct Tag会使情况变好:
Age int `json:"how-old"`
其中json:"how-old"
为Struct Tag, 完整代码如下:
package main
import (
"log"
"encoding/json"
)
type Person struct {
Name string
Age int `json:"how-old"`
Email []string
}
var json_str = []byte(`{"name":"lane","how-old":18,"email":["xxx@qq.com","yyy@qq.com"]}`)
func main() {
p := Person{}
err := json.Unmarshal(json_str, &p)
if err != nil {
log.Print(err)
} else {
log.Print(p)
}
p.Age = 19
json_str, err := json.Marshal(p)
if err != nil {
log.Print(err)
} else {
log.Print(string(json_str))
}
}
运行结果为:
2021/07/06 12:23:07 {lane 18 [xxx@qq.com yyy@qq.com]}
2021/07/06 12:23:07 {"Name":"lane","how-old":19,"Email":["xxx@qq.com","yyy@qq.com"]}
4 使用omitempty
如果把p.Email设为nil,
p.Age = 19
p.Email = nil // <--- add this line
则运行结果为:
2021/07/07 04:28:51 {lane 18 [xxx@qq.com yyy@qq.com]}
2021/07/07 04:28:51 {"Name":"lane","how-old":19,"Email":null}
其中,也可以省略掉"Email":null
方法是:
type Person struct {
Name string
Age int `json:"how-old"`
Email []string `json:"email,omitempty"` // use omitempty
}
下面是完整代码:
package main
import (
"log"
"encoding/json"
)
type Person struct {
Name string
Age int `json:"how-old"`
Email []string `json:"email,omitempty"`
}
var json_str = []byte(`{"name":"lane","how-old":18,"email":["xxx@qq.com","yyy@qq.com"]}`)
func main() {
p := Person{}
err := json.Unmarshal(json_str, &p)
if err != nil {
log.Print(err)
} else {
log.Print(p)
}
p.Age = 19
p.Email = nil
json_str, err := json.Marshal(p)
if err != nil {
log.Print(err)
} else {
log.Print(string(json_str))
}
}
运行结果:
2021/07/07 04:33:46 {lane 18 [xxx@qq.com yyy@qq.com]}
2021/07/07 04:33:46 {"Name":"lane","how-old":19}
网友评论