当前位置:网站首页>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}]}
边栏推荐
- 深度学习的随机种子
- 指令集数据产品如何设计和实现报表协同系统——基于指令集物联网操作系统的工业协同制造项目开发实践
- Design for failure常见的12种设计思想
- The difference and usage of call, apply and bind
- Framework 到底该怎么学习?
- vim相关介绍(三)
- 【leetcode】The sword refers to Offer II 002. Binary addition
- mysql使用on duplicate key update批量更新数据
- JetsonNano learning (6) Big pits and solutions that Jetson stepped on___Continuously updated
- MySQL【基本select语句】
猜你喜欢
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
The latest Gansu construction welder (construction special operation) simulation question bank and answer analysis in 2022
彻底搞懂kubernetes调度框架与插件
关于 byte 的范围
devops学习(五) Jenkins 简单完成持续部署
Tkinter:功能按钮Button
Raspberry pie wiringPi 2.6 installed on solving gpio readall command mistakes
MySQL事务(transaction) (有这篇就足够了..)
devops学习(八) 搭建镜像仓库---jenkins推送镜像
Access Modbus TCP and Modbus RTU protocol devices using Neuron
随机推荐
devops学习(四) Jenkins CI 持续集成
C陷阱与缺陷 第3章 语义“陷阱” 3.10 为函数main提供返回值
[leetcode] 80. Delete duplicates in sorted array II (medium) (double pointer, in-place modification)
第一范式、第二范式、第三范式
【无标题】清空吗
jenkins use and maintenance
关于MySQL索引的一些个人理解(部分参考MySQL45讲)
SQL Server、MySQL主从搭建,EF Core读写分离代码实现
The basic parallel I/O port of single chip microcomputer development
读书笔记:《这才是心理学:看穿伪心理学的本质(第10版)》
y81.第四章 Prometheus大厂监控体系及实战 -- 监控扩展(十二)
Brute force recursion to dynamic programming 04 (digital string conversion)
C陷阱与缺陷 第4章 链接 4.1 什么是链接器
MySQL六脉神剑,SQL通关大总结
【openlayers】Map【1】
MySQL【基本select语句】
【无标题】
437. 路径总和 III ●●
437. The total path III low low
全国双非院校考研信息汇总整理 Part.3