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

相关文章
|
JSON 缓存 自然语言处理
多语言实时数据微店商品详情API:技术实现与JSON数据解析指南
通过以上技术实现与解析指南,开发者可高效构建支持多语言的实时商品详情系统,满足全球化电商场景需求。
|
11月前
|
XML JSON 编解码
重新认识 Golang 中的 json 编解码
欢迎访问[莹的网络日志](https://lifukun.com),分享技术探索与思考。本文深入解析Go中json编解码特性,涵盖字段映射、omitempty行为、性能对比、自定义编解码及json/v2新特性,助你真正掌握json使用细节。
|
JSON 自然语言处理 API
多语言实时数据淘宝商品评论API:技术实现与JSON数据解析指南
淘宝商品评论多语言实时采集需结合官方API与后处理技术实现。建议优先通过地域站点适配获取本地化评论,辅以机器翻译完成多语言转换。在合规前提下,企业可构建多语言评论数据库,支撑全球化市场分析与产品优化。
|
JSON 人工智能 Go
在Golang中序列化JSON字符串的教程
在Golang中,使用`json.Marshal()`可将数据结构序列化为JSON格式。若直接对JSON字符串进行序列化,会因转义字符导致错误。解决方案包括使用`[]byte`或`json.RawMessage()`来避免双引号被转义,从而正确实现JSON的序列化与反序列化。
785 7
|
数据采集 JSON 数据可视化
JSON数据解析实战:从嵌套结构到结构化表格
在信息爆炸的时代,从杂乱数据中提取精准知识图谱是数据侦探的挑战。本文以Google Scholar为例,解析嵌套JSON数据,提取文献信息并转换为结构化表格,通过Graphviz制作技术关系图谱,揭示文献间的隐秘联系。代码涵盖代理IP、请求头设置、JSON解析及可视化,提供完整实战案例。
969 4
JSON数据解析实战:从嵌套结构到结构化表格
|
存储 监控 Go
面向OpenTelemetry的Golang应用无侵入插桩技术
文章主要讲述了阿里云 ARMS 团队与程序语言与编译器团队合作研发的面向OpenTelemetry的Golang应用无侵入插桩技术解决方案,旨在解决Golang应用监控的挑战。
1429 9
|
JSON Go 数据格式
Golang语言结构体链式编程与JSON序列化
这篇文章是关于Go语言中结构体链式编程与JSON序列化的教程,详细介绍了JSON格式的基本概念、结构体的序列化与反序列化、结构体标签的使用以及如何实现链式编程。
422 4
|
JSON Java 数据格式
Java系列之:如何取出嵌套JSON中的数据值
这篇文章介绍了如何在Java中取出嵌套JSON数据值的方法,通过使用`JSONObject`类及其`getJSONObject`和`get`方法来逐步解析和提取所需的数据。
Java系列之:如何取出嵌套JSON中的数据值
|
监控 Serverless Go
Golang 开发函数计算问题之Go 语言中切片扩容时需要拷贝原数组中的数据如何解决
Golang 开发函数计算问题之Go 语言中切片扩容时需要拷贝原数组中的数据如何解决
236 0

热门文章

最新文章

推荐镜像

更多