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

相关文章
|
7月前
|
JSON API 数据格式
淘宝拍立淘按图搜索API系列,json数据返回
淘宝拍立淘按图搜索API系列通过图像识别技术实现商品搜索功能,调用后返回的JSON数据包含商品标题、图片链接、价格、销量、相似度评分等核心字段,支持分页和详细商品信息展示。以下是该API接口返回的JSON数据示例及详细解析:
|
7月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
7月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
7月前
|
XML JSON 编解码
重新认识 Golang 中的 json 编解码
欢迎访问[莹的网络日志](https://lifukun.com),分享技术探索与思考。本文深入解析Go中json编解码特性,涵盖字段映射、omitempty行为、性能对比、自定义编解码及json/v2新特性,助你真正掌握json使用细节。
|
7月前
|
JSON 中间件 Java
【GoGin】(3)Gin的数据渲染和中间件的使用:数据渲染、返回JSON、浅.JSON()源码、中间件、Next()方法
我们在正常注册中间件时,会打断原有的运行流程,但是你可以在中间件函数内部添加Next()方法,这样可以让原有的运行流程继续执行,当原有的运行流程结束后再回来执行中间件内部的内容。​ c.Writer.WriteHeaderNow()还会写入文本流中。可以看到使用next后,正常执行流程中并没有获得到中间件设置的值。接口还提供了一个可以修改ContentType的方法。判断了传入的状态码是否符合正确的状态码,并返回。在内部封装时,只是标注了不同的render类型。再看一下其他返回的类型;
363 3
|
7月前
|
JSON Java Go
【GoGin】(2)数据解析和绑定:结构体分析,包括JSON解析、form解析、URL解析,区分绑定的Bind方法
bind或bindXXX函数(后文中我们统一都叫bind函数)的作用就是将,以方便后续业务逻辑的处理。
450 3
|
9月前
|
JSON API 数据安全/隐私保护
深度分析淘宝卖家订单详情API接口,用json返回数据
淘宝卖家订单详情API(taobao.trade.fullinfo.get)是淘宝开放平台提供的重要接口,用于获取单个订单的完整信息,包括订单状态、买家信息、商品明细、支付与物流信息等,支撑订单管理、ERP对接及售后处理。需通过appkey、appsecret和session认证,并遵守调用频率与数据权限限制。本文详解其使用方法并附Python调用示例。
|
8月前
|
机器学习/深度学习 JSON 监控
淘宝拍立淘按图搜索与商品详情API的JSON数据返回详解
通过调用taobao.item.get接口,获取商品标题、价格、销量、SKU、图片、属性、促销信息等全量数据。
|
8月前
|
JSON 缓存 自然语言处理
多语言实时数据微店商品详情API:技术实现与JSON数据解析指南
通过以上技术实现与解析指南,开发者可高效构建支持多语言的实时商品详情系统,满足全球化电商场景需求。
|
8月前
|
JSON API 数据格式
干货满满!淘宝商品详情数据,淘宝API(json数据返回)
淘宝商品详情 API 接口(如 taobao.item.get)的 JSON 数据返回示例如下

热门文章

最新文章

推荐镜像

更多