当前位置:网站首页>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}]}
边栏推荐
- [2023 School Recruitment Questions] Summary of knowledge points and hand-tear code in the written test and interview
- EA&UML日拱一卒-状态图::重画按钮状态图
- 能源企业数字化转型背景下的数据安全治理实践路径
- Adaptive feature fusion pyramid network for multi-classes agriculturalpest detection
- 高数下|三重积分习题课|高数叔|手写笔记
- The latest Gansu construction welder (construction special operation) simulation question bank and answer analysis in 2022
- EA & UML Sun Gong Yip - Multitasking Programming Super Introductory - (7) About mutex, you must know
- jenkins use and maintenance
- Another new rule for credit cards is coming!Juphoon uses technology to boost the financial industry to improve service quality and efficiency
- USACO2008通信线路
猜你喜欢

EA&UML日拱一卒-多任务编程超入门-(2)进程和线程

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

Elephant Swap:借助ePLATO提供加密市场的套利空间

接口性能测试方案设计方法有哪些?要怎么去写?

EA&UML日拱一卒-多任务编程超入门-(8)多任务安全的数据类

UE4 制作十字准心+后坐力

jenkins搭建部署详细步骤

软件测试拿8k以上有多简单,掌握这些随随便便拿8k以上...

devops学习(八) 搭建镜像仓库---jenkins推送镜像

High Numbers|Calculation of Triple Integral 3|Uncle High Numbers|Handwritten Notes
随机推荐
接口测试的概念、目的、流程、测试方法有哪些?
深度学习的随机种子
全国双非院校考研信息汇总整理 Part.5
devops学习(六)Jenkins 持续部署-版本选择
暴力递归到动态规划 03 (背包问题)
Vulkan与OpenGL对比——Vulkan的全新渲染架构
UE4 makes crosshair + recoil
DFS对树的遍历及一些优化
[leetcode] The sword refers to Offer II 006. The sum of two numbers in a sorted array (binary search, double pointer)
种类并查集(反集),学习T宝代码
乐理&吉他技巧
devops学习(四) Jenkins CI 持续集成
JetsonNano learning (6) Big pits and solutions that Jetson stepped on___Continuously updated
shell编写规范和变量
Prometheus 的功能特性
Docker install MySQL8.0
SQL Server、MySQL主从搭建,EF Core读写分离代码实现
【云原生Kubernetes】二进制搭建Kubernetes集群(中)——部署node节点
rk-boot框架实战(1)
一文解答web性能优化