如何从空的 interface{} 类型访问 JSON 数据中的字段?
来源:stackoverflow
时间:2024-04-04 20:57:39 298浏览 收藏
大家好,我们又见面了啊~本文《如何从空的 interface{} 类型访问 JSON 数据中的字段?》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~
我是 Go 新手,我知道这可能是一个简单的答案,但我在空接口中访问 JSON 值时遇到了困难。数据来自 Twilio Go Helper 库 (https://pkg.go.dev/github.com/twilio/[电子邮件受保护] /rest/lookups/v2#LookupsV2PhoneNumber.LineTypeIntelligence) 并且我正在尝试访问对象中的值
LineTypeIntelligence *接口{} `json:"line_type_intelligence,omitempty"`
如果我打印对象,它会显示为地图
fmt.Println(*resp.LineTypeIntelligence)
哪个 输出
map[运营商名称:Verizon Wireless error_code:
我尝试使用直接访问地图中的值
Carrier_name := resp.LineTypeIntelligence["Carrier_name"].(string)
不幸的是,这会产生编译器错误:
in无效操作:无法索引 resp.LineTypeIntelligence(*interface{} 类型的变量)
正确答案
就目前而言:
carrier_name := resp.linetypeintelligence["carrier_name"].(string)
这是行不通的,因为 resp.linetypeintelligence
的类型为 *interface{}
(或简称 *any
)。您无法像访问地图一样访问它。 interface{}
类型简单地表示无论为该字段分配什么值,它都会实现一个空接口(也就是说:所有东西都实现一个包含 0 个方法的接口,因此该值可以是任何值)。
要访问数据本身,您需要一些类型断言/转换,并将值复制到更易于使用的映射中:
ltimap, ok := *resp.linetypeintelligence.(map[string]any) if !ok { // return errors.errorf("expected linetypeintelligence to be a map, instead saw %t (%#v)", *resp.linetypeintelligence, *resp.linetypeintelligence) panic("handle error because we expected the value to be a map, but it's something else") } // now ltimap is map[string]any carriername, ok := ltimap["carrier_name"].(string) if !ok { return errors.ner("carrier name not a string") } // and so on
这当然相当乏味。考虑到您已经知道地图将包含 carrier_name
键,为什么不将数据解组为不同的类型?
查看数据,这应该有效:
type lti struct { carriername string `json:"carrier_name"` errcode *int `json:"error_code,omitempty"` // or string, or whatever mobcc int `json:"mobile_country_code"` // twilio might use string for these mobnc int `json:"mobile_network_code"` type string `json:"type"` }
这不是最有效的处理方法,但将此数据放入您自己的对象的最快方法是再次封送它,并以您自己的类型解封它:
b, _ := json.marshal(resp.linetypeintelligence) lti := lti{} if err := json.unmarshal(b, <i); err != nil { // handle error } fmt.printf("carrier name: %s\n", lti.carriername)
也许是迂腐的,但 golang 经常被批评为“过于固执己见” wrt 你应该如何格式化/编写代码。这种批评已经平息下来,因为严格的格式已被证明是一件好事。由于代码的格式相同,因此人们可以查看任何人编写的代码,并关注它做了什么,而不是它是如何编写的。
由于跨项目/代码库一致性的好处,除了 gofmt
之外,golang 存储库还有一个涵盖大量其他约定的 wiki 条目:Code Review Comments
您的问题包含 carrier_name
等变量。在golang中,推荐使用snakecase
变量名。
您还可以执行类似 resp.linetypeintelligence["rier_name"].(string)
的操作。但是,从地图中获取值不应与强制转换结合使用。安全地图访问如下所示:
v, ok := m["carrier_name"] if !ok { // v will be the nil value of the value type of your map // ie map[string]int => v == 0 // map[string]string => v == "" // map[string]*int => v == nil // you can initialise v to a safe value if needed, or return an error } // ok was true, the map does indeed contain a value for the key "carrier_name" name, ok := v.(string) if !ok { // though the map contained the required key, it turned out not to be a string... } // type assertion was successful: map contained the key, and the value was indeed a string
今天关于《如何从空的 interface{} 类型访问 JSON 数据中的字段?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习