Go语言利用Unmarshal解析json字符串的实现
作者:hp_cpp 时间:2024-05-09 09:54:25
简单的解析例子:
首先还是从官方文档中的例子:
package main
import (
?? ?"fmt"
?? ?"encoding/json"
)
type Animal struct {
? ? Name ?string
? ? Order string
}
func main() {
?? ?var jsonBlob = []byte(`[
?? ??? ?{"Name": "Platypus", "Order": "Monotremata"},
?? ??? ?{"Name": "Quoll", ? ?"Order": "Dasyuromorphia"}
?? ?]`)
?? ?var animals []Animal
?? ?
?? ?err := json.Unmarshal(jsonBlob, &animals)
?? ?if err != nil {
?? ? ? ?fmt.Println("error:", err)
?? ?}
?? ?fmt.Printf("%+v", animals)
}
输出:
[{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
简单进行修改,修改为:
package main
import (
?? ?"fmt"
?? ?"encoding/json"
)
type Animal struct {
? ? Name ?string
? ? Order string
}
func main() {
?? ?var jsonBlob = []byte(`{"Name": "Platypus", "Order": "Monotremata"}`)
?? ?var animals Animal
?? ?err := json.Unmarshal(jsonBlob, &animals)
?? ?if err != nil {
?? ? ? ?fmt.Println("error:", err)
?? ?}
?? ?fmt.Printf("%+v", animals)
}
输出:
{Name:Platypus Order:Monotremata}
还是之前的例子:
解析这样的一个json字符串:
{
? ? "first fruit":
? ? {
? ? ? ? "describe":"an apple",
? ? ? ? "icon":"appleIcon",
? ? ? ? "name":"apple"
? ? },
? ? "second fruit":
? ? {
? ? ? ? "describe":"an orange",
? ? ? ? "icon":"orangeIcon",
? ? ? ? "name":"orange"
? ? },
? ? "three fruit array":
? ? [
? ? ? ? "eat 0",
? ? ? ? "eat 1",
? ? ? ? "eat 2",
? ? ? ? "eat 3",
? ? ? ? "eat 4"
? ? ]
}
go代码:
package main
import (
?? ?"fmt"
?? ?"encoding/json"
)
type Fruit struct {
?? ?Describe string `json:"describe"`
?? ?Icon ? ? string `json:"icon"`
?? ?Name ? ? string `json:"name"`
}
type FruitGroup struct {
?? ?FirstFruit ?*Fruit `json:"first fruit"` ?//指针,指向引用对象;如果不用指针,只是值复制
?? ?SecondFruit *Fruit `json:"second fruit"` //指针,指向引用对象;如果不用指针,只是值复制
?? ?THreeFruitArray []string `json:"three fruit array"`
}
func main() {
?? ?var jsonBlob = []byte(`{
? ? "first fruit": {
? ? ? ? "describe": "an apple",
? ? ? ? "icon": "appleIcon",
? ? ? ? "name": "apple"
? ? },
? ? "second fruit": {
? ? ? ? "describe": "an orange",
? ? ? ? "icon": "appleIcon",
? ? ? ? "name": "orange"
? ? },
? ? "three fruit array": [
? ? ? ? "eat 0",
? ? ? ? "eat 1",
? ? ? ? "eat 2",
? ? ? ? "eat 3"
? ? ]}`)
?? ?var fruitGroup FruitGroup
?? ?
?? ?err := json.Unmarshal(jsonBlob, &fruitGroup)
?? ?if err != nil {
?? ? ? ?fmt.Println("error:", err)
?? ?}
?? ?fmt.Printf("%+v\n", fruitGroup)
?? ?fmt.Printf("%+v\n", fruitGroup.FirstFruit)
?? ?fmt.Printf("%+v\n", fruitGroup.SecondFruit)
}
运行结果:
{FirstFruit:0xc00006c5a0 SecondFruit:0xc00006c5d0 THreeFruitArray:[eat 0 eat 1 eat 2 eat 3]}
&{Describe:an apple Icon:appleIcon Name:apple}
&{Describe:an orange Icon:appleIcon Name:orange}
来源:https://blog.csdn.net/hp_cpp/article/details/101058472
标签:Go,Unmarshal,解析,json
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中选择结构实例讲解
2023-06-26 14:56:09
用uWSGI和Nginx部署Flask项目的方法示例
2023-12-07 22:42:37
![](https://img.aspxhome.com/file/2023/2/110372_0s.jpg)
mysql 8.0.15 winx64压缩包安装配置方法图文教程
2024-01-23 03:00:13
![](https://img.aspxhome.com/file/2023/8/65278_0s.jpg)
mysql存储过程基础之遍历多表记录后插入第三方表中详解
2024-01-24 12:59:27
![](https://img.aspxhome.com/file/2023/9/112339_0s.png)
关于mysql与mysqli
2011-02-23 12:03:00
Python for Informatics 第11章之正则表达式(四)
2023-06-15 04:48:39
Python中pyecharts安装及安装失败的解决方法
2021-01-13 06:00:52
![](https://img.aspxhome.com/file/2023/6/99656_0s.jpg)
自定义asp错误信息的显示
2007-09-13 12:33:00
mysql运行net start mysql报服务名无效的解决办法
2024-01-17 06:12:15
![](https://img.aspxhome.com/file/2023/6/108876_0s.jpg)
Python3 实现随机生成一组不重复数并按行写入文件
2021-11-25 18:05:35
python http通信接口开发示例
2022-06-07 05:15:29
![](https://img.aspxhome.com/file/2023/9/96989_0s.png)
Python中的字符串类型基本知识学习教程
2023-03-29 01:48:47
浅谈PHP错误类型及屏蔽方法
2023-11-23 10:26:46
Python中实现输入一个整数的案例
2022-05-28 18:42:21
Python数据分析 Pandas Series对象操作
2021-07-17 20:18:52
![](https://img.aspxhome.com/file/2023/4/99664_0s.png)
pytorch常见的Tensor类型详解
2022-10-25 19:34:01
Python制作数据导入导出工具
2023-07-25 06:59:59
Pytorch中的variable, tensor与numpy相互转化的方法
2021-08-16 13:59:50
![](https://img.aspxhome.com/file/2023/5/101625_0s.png)
SQLServer:探讨EXEC与sp_executesql的区别详解
2024-01-29 02:53:53
![](https://img.aspxhome.com/file/2023/8/122398_0s.png)
做设计还是做产品
2009-06-11 13:01:00