技术经验分享: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}

相关文章
|
2月前
|
JSON API 数据格式
淘宝拍立淘按图搜索API系列,json数据返回
淘宝拍立淘按图搜索API系列通过图像识别技术实现商品搜索功能,调用后返回的JSON数据包含商品标题、图片链接、价格、销量、相似度评分等核心字段,支持分页和详细商品信息展示。以下是该API接口返回的JSON数据示例及详细解析:
|
2月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
2月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
2月前
|
JSON 中间件 Java
【GoGin】(3)Gin的数据渲染和中间件的使用:数据渲染、返回JSON、浅.JSON()源码、中间件、Next()方法
我们在正常注册中间件时,会打断原有的运行流程,但是你可以在中间件函数内部添加Next()方法,这样可以让原有的运行流程继续执行,当原有的运行流程结束后再回来执行中间件内部的内容。​ c.Writer.WriteHeaderNow()还会写入文本流中。可以看到使用next后,正常执行流程中并没有获得到中间件设置的值。接口还提供了一个可以修改ContentType的方法。判断了传入的状态码是否符合正确的状态码,并返回。在内部封装时,只是标注了不同的render类型。再看一下其他返回的类型;
181 3
|
2月前
|
JSON Java Go
【GoGin】(2)数据解析和绑定:结构体分析,包括JSON解析、form解析、URL解析,区分绑定的Bind方法
bind或bindXXX函数(后文中我们统一都叫bind函数)的作用就是将,以方便后续业务逻辑的处理。
283 3
|
2月前
|
存储 安全 Java
【Golang】(4)Go里面的指针如何?函数与方法怎么不一样?带你了解Go不同于其他高级语言的语法
结构体可以存储一组不同类型的数据,是一种符合类型。Go抛弃了类与继承,同时也抛弃了构造方法,刻意弱化了面向对象的功能,Go并非是一个传统OOP的语言,但是Go依旧有着OOP的影子,通过结构体和方法也可以模拟出一个类。
201 1
|
Go
Golang语言之管道channel快速入门篇
这篇文章是关于Go语言中管道(channel)的快速入门教程,涵盖了管道的基本使用、有缓冲和无缓冲管道的区别、管道的关闭、遍历、协程和管道的协同工作、单向通道的使用以及select多路复用的详细案例和解释。
649 4
Golang语言之管道channel快速入门篇
|
Go
Golang语言文件操作快速入门篇
这篇文章是关于Go语言文件操作快速入门的教程,涵盖了文件的读取、写入、复制操作以及使用标准库中的ioutil、bufio、os等包进行文件操作的详细案例。
251 4
Golang语言文件操作快速入门篇
|
Go
Golang语言之gRPC程序设计示例
这篇文章是关于Golang语言使用gRPC进行程序设计的详细教程,涵盖了RPC协议的介绍、gRPC环境的搭建、Protocol Buffers的使用、gRPC服务的编写和通信示例。
549 3
Golang语言之gRPC程序设计示例
|
安全 Go
Golang语言goroutine协程并发安全及锁机制
这篇文章是关于Go语言中多协程操作同一数据问题、互斥锁Mutex和读写互斥锁RWMutex的详细介绍及使用案例,涵盖了如何使用这些同步原语来解决并发访问共享资源时的数据安全问题。
311 4