技术经验分享:Golang如何解组嵌套的JSON数据的子集

简介: 技术经验分享:Golang如何解组嵌套的JSON数据的子集

{


"coord": {


"lon": -0.13,


"lat": 51.51


},


"weather": 【


{


"id": 300,


"main": "Drizzle",


"description": "light intensity drizzle",


"icon": "09d"


}


】,


"base": "stations",


"main": {


"temp": 280.32,


"pressure": 1012,


"humidity": 81,


"temp_min": 279.15,


"temp_max": 281.15


},


"visibility": 10000,


"wind": {


"speed": 4.1,


"deg": 80


},


"clouds": {


"all": 90


},


"dt": 1485789600,


"sys": {


"type": 1,


"id": 5091,


"message": 0.0103,


"country": "GB",


"sunrise": 1485762037,


"sunset": 1485794875


},


"id": 2643743,


"name": "London",


"cod": 200


}


要像这样的天气概述结构


type Weather struct {


Location string


Weather string


Description string


Temperature float32


MinTemperature float32


MaxTemperature float32


}


使用标准的JSON包,我们将解组它,然后像这样重组它


type Weather struct {


Location string


Weather //代码效果参考:http://www.lyjsj.net.cn/wz/art_22998.html

string

Description string


Temperature float32


MinTemperature float32


MaxTemperature float32


}


type TmpWeather struct {


Location string json:"name"


Weather 【】struct {


Weather string json:"main"


Description string json:"description"


} json:"weather"


Temperature struct {


Temperature float32 json:"temp"


MinTemperature float32 json:"temp_min"


MaxTemperature float32 json:"temp_max"


} json:"main"


}


var tmpW TmpWeather


err := json.Unmarshal(【】byte(jsonString), &tmpW)


if err != nil {


panic(err)


}


fmt.Printf("%+v\n", tmpW)


// {Location:London Weather:【{Weather:Drizzle Description:light intensity drizzle}】 Temperature:{Temperature:280.32 MinTemperature:279.15 MaxTemperature:281.15}}


weather := Weather{


Location: tmpW.Location,


Weather: tmpW.Weather【0】.Weather,


Description: tmpW.Weather【0】.Description,


Temperature: tmpW.Temperature.Temperature,


MinTemperature: tmpW.Temperature.MinTemperature,


MaxTemperature: tmpW.Temperature.MaxTemperature,


}


fmt.Printf("%+v\n", weather)


// {Location:London Weather:Drizzle Description:light intensity drizzle Temperature:280.32 MinTemperature:279.15 MaxTemperature:281.15}


将njson标记添加到struct,


然后像这样使用NJSON解组


type Weather struct {


Location string njson:"name"


Weather string njson:"weather.0.main"


Description string njson:"weather.0.description"


Temperature float32 njson:"main.temp"


MinTemperature float32 njson:"main.temp_min"


MaxTemperature float32 njson:"main.temp_max"


}


var weather Weather


err := njson.Unmarshal(【】byte(jsonString), &weather)


if err != nil {


panic(err)


}


fmt.Printf("%+v\n", weather)


// {Location:London Weather:Drizzle Description:light intensity drizzle Temperature:280.32 MinTemperature:279.15 MaxTemperature:281.15}

相关文章
|
21天前
|
JSON API 数据格式
淘宝商品评论API接口,json数据示例参考
淘宝开放平台提供了多种API接口来获取商品评论数据,其中taobao.item.reviews.get是一个常用的接口,用于获取指定商品的评论信息。以下是关于该接口的详细介绍和使用方法:
|
2月前
|
SQL JSON 数据格式
SPL 处理多层 JSON 数据比 DuckDB 方便多了
esProc SPL 处理多层 JSON 数据比 DuckDB 更便捷,尤其在保留 JSON 层次与复杂计算时优势明显。DuckDB 虽能通过 `read_json_auto()` 将 JSON 解析为表格结构,但面对深层次或复杂运算时,SQL 需频繁使用 UNNEST、子查询等结构,逻辑易变得繁琐。而 SPL 以集合运算方式直接处理子表,代码更简洁直观,无需复杂关联或 Lambda 语法,同时保持 JSON 原始结构。esProc SPL 开源免费,适合复杂 JSON 场景,欢迎至乾学院探索!
|
24天前
|
JSON 定位技术 PHP
PHP技巧:解析JSON及提取数据
这就是在PHP世界里探索JSON数据的艺术。这场狩猎不仅仅是为了获得数据,而是一种透彻理解数据结构的行动,让数据在你的编码海洋中畅游。通过这次冒险,你已经掌握了打开数据宝箱的钥匙。紧握它,让你在编程世界中随心所欲地航行。
124 67
|
28天前
|
JSON 前端开发 应用服务中间件
配置Nginx根据IP地址进行流量限制以及返回JSON格式数据的方案
最后,记得在任何生产环境部署之前,进行透彻测试以确保一切运转如预期。遵循这些战术,守卫你的网络城堡不再是难题。
67 3
|
3月前
|
XML JSON API
如何在 Postman 中上传文件和 JSON 数据
如果你想在 Postman 中同时上传文件和 JSON 数据,本文将带你一步一步地了解整个过程,包括最佳实践和技巧,让你的工作更轻松。
|
10月前
|
Go
Golang语言之管道channel快速入门篇
这篇文章是关于Go语言中管道(channel)的快速入门教程,涵盖了管道的基本使用、有缓冲和无缓冲管道的区别、管道的关闭、遍历、协程和管道的协同工作、单向通道的使用以及select多路复用的详细案例和解释。
308 4
Golang语言之管道channel快速入门篇
|
10月前
|
Go
Golang语言文件操作快速入门篇
这篇文章是关于Go语言文件操作快速入门的教程,涵盖了文件的读取、写入、复制操作以及使用标准库中的ioutil、bufio、os等包进行文件操作的详细案例。
140 4
Golang语言文件操作快速入门篇
|
10月前
|
Go
Golang语言之gRPC程序设计示例
这篇文章是关于Golang语言使用gRPC进行程序设计的详细教程,涵盖了RPC协议的介绍、gRPC环境的搭建、Protocol Buffers的使用、gRPC服务的编写和通信示例。
278 3
Golang语言之gRPC程序设计示例
|
10月前
|
安全 Go
Golang语言goroutine协程并发安全及锁机制
这篇文章是关于Go语言中多协程操作同一数据问题、互斥锁Mutex和读写互斥锁RWMutex的详细介绍及使用案例,涵盖了如何使用这些同步原语来解决并发访问共享资源时的数据安全问题。
201 4
|
9月前
|
前端开发 中间件 Go
实践Golang语言N层应用架构
【10月更文挑战第2天】本文介绍了如何在Go语言中使用Gin框架实现N层体系结构,借鉴了J2EE平台的多层分布式应用程序模型。文章首先概述了N层体系结构的基本概念,接着详细列出了Go语言中对应的构件名称,包括前端框架(如Vue.js、React)、Gin的处理函数和中间件、依赖注入和配置管理、会话管理和ORM库(如gorm或ent)。最后,提供了具体的代码示例,展示了如何实现HTTP请求处理、会话管理和数据库操作。
126 1