当前位置:网站首页>Go language serialization and deserialization and how to change the uppercase of the json key value to lowercase if the json after serialization is empty
Go language serialization and deserialization and how to change the uppercase of the json key value to lowercase if the json after serialization is empty
2022-07-29 23:54:00 【ydl1128】
go语言序列化
定义嵌套结构体
type Animal struct {
Name string
}
type Person2 struct {
Name string
Age int
Animal Animal
}
Use serialized packages
func main() {
p:= Person2{
Name: "小花",
Age: 121,
Animal: Animal{
Name: "旺财",
},
}
json, err := json.Marshal(&p)
if err !=nil{
panic("error!!!")
}
fmt.Printf(string(json))
}
结果:
{“Name”:“小花”,“Age”:121,“Animal”:{“Name”:“旺财”}}
关于goafter serializationjsondata is empty andjson数据的keyIt's a matter of how to change uppercase to lowercase
Assume in the above struct,We start lowercase for all member variables,只有Animal是大写.Then the value after serialization is:
{“”,“Animal”:{}}.
在goVariables in the language that start with lowercase are private,Variables starting with a capital letter are shared.把personPass in the serialized package,Then it's not the same package,因此访问不到,Serialization is thatnull.
解决办法:
Change the variable name to uppercase.
优化:Because capitalization does not conform to the norm of writing.So we want to get the property name in lower casejson数据.So how to do it?
//Add it after the corresponding property name`json:"字段名"`.The field name here is after parsingjson的key值
type Person2 struct {
Name string `json:"name"`
Age int `json:"age"`
Animal Animal
}
运行结果:
{“name”:“小花”,“age”:121,“Animal”:{“Name”:“旺财”}}.
so 神奇!
goLanguage deserialization
调用Unmarshal包,Help me convert to the specified structure data
Let's make a small change to the structure first:
type Person2 struct {
Name string `json:"name"`
Age int `json:"age"`
Animal []Animal
}
Deserialize into the specified structure:
str := `{"Name":"小花","Age":121,"Animal":[{"Name":"旺财"},{"Name":"旺财2"}]}`
c1 := &Person2{
}
err := json.Unmarshal([]byte(str), c1)
if err != nil {
fmt.Println("json unmarshal failed!")
return
}
fmt.Printf("%v",*c1)
打印的结果:
{小花 121 [{旺财} {旺财2}]}
边栏推荐
- devops学习(四) Jenkins CI 持续集成
- 2022/7/29 考试总结
- 多商户商城系统功能拆解18讲-平台端商家售后
- C陷阱与缺陷 第5章 库函数 5.3 缓冲输出与内存分配
- 能源企业数字化转型背景下的数据安全治理实践路径
- 全国双非院校考研信息汇总整理 Part.2
- 学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
- [leetcode] The sword refers to Offer II 006. The sum of two numbers in a sorted array (binary search, double pointer)
- DFS对树的遍历及一些优化
- codeforces 线段树题单
猜你喜欢

Install PyCharm on Raspberry Pi

Farmers on the assembly line: I grow vegetables in a factory

从面试官角度分析:面试功能测试工程师主要考察哪些能力?

决策树原理及代码实现

devops学习(十) Jenkins 流水线

EA & UML Sun Gong Yip - Multitasking Programming Super Introductory - (7) About mutex, you must know

Access Modbus TCP and Modbus RTU protocol devices using Neuron

Codeforces Round #805 (Div. 3)总结

【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史

MySQL函数(经典收藏)
随机推荐
[leetcode] 80. Delete duplicates in sorted array II (medium) (double pointer, in-place modification)
【leetcode】The sword refers to Offer II 002. Binary addition
vim相关介绍(三)
[2023 School Recruitment Questions] Summary of knowledge points and hand-tear code in the written test and interview
leetcode122. Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II(简单)
Design for failure常见的12种设计思想
单片机开发之拓展并行I/O口
C陷阱与缺陷 第4章 链接 4.4 形参、实参与返回值
call、apply 以及 bind 的区别和用法
18 Lectures on Disassembly of Multi-merchant Mall System Functions
High - level - the rest - the client determine whether indexes exist
桌面软件开发框架大赏
全国双非院校考研信息汇总整理 Part.2
容器化 | 在 Rancher 中部署 MySQL 集群
y81. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Monitoring Extension (12)
2022/7/29 考试总结
devops学习(九) Helm工具--持续部署
C陷阱与缺陷 第5章 库函数 5.2更新顺序文件
Windows 安装 MySQL 5.7详细步骤
EA&UML日拱一卒-多任务编程超入门-(2)进程和线程