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

相关文章
|
3天前
|
存储 JSON NoSQL
深入解析RedisJSON:在Redis中直接处理JSON数据
深入解析RedisJSON:在Redis中直接处理JSON数据
|
3天前
|
SQL NoSQL Go
技术经验分享:Golang标准库:errors包应用
技术经验分享:Golang标准库:errors包应用
|
3天前
|
JSON 前端开发 数据格式
SpringMVC的数据响应-直接回写json字符串
SpringMVC的数据响应-直接回写json字符串
|
12天前
|
存储 JSON JavaScript
【chat-gpt问答记录】python将数据存为json格式和yaml格式
【chat-gpt问答记录】python将数据存为json格式和yaml格式
26 1
|
22天前
|
存储 JSON 分布式计算
DataWorks产品使用合集之如何在数据服务中处理JSON数据
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
41 11
|
17天前
|
存储 JSON JavaScript
使用Python处理JSON格式数据
使用Python处理JSON格式数据
|
25天前
|
JSON JavaScript IDE
JSON 数据格式化方法
JSON 数据格式化方法
32 3
|
18天前
|
JSON JavaScript 测试技术
掌握JMeter:深入解析如何提取和利用JSON数据
Apache JMeter教程展示了如何提取和使用JSON数据。创建测试计划,包括HTTP请求和JSON Extractor,设置变量前缀和JSON路径表达式来提取数据。通过Debug Sampler和View Results Tree监听器验证提取结果,然后在后续请求和断言中使用这些数据。此方法适用于复杂测试场景,提升性能和自动化测试效率。
29 0
|
14天前
|
JSON 关系型数据库 MySQL
实时计算 Flink版产品使用问题之在使用CDAS语法同步MySQL数据到Hologres时,如果开启了字段类型宽容模式,MySQL中的JSON类型会被转换为什么
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
18天前
|
JSON 数据格式 Python
python3 服务端使用CGI脚本处理POST的Json数据
python3 服务端使用CGI脚本处理POST的Json数据
29 6