我的代码中没有JSON解组输出,但在goplayground中可以正常工作
来源:stackoverflow
时间:2024-02-26 08:18:24 264浏览 收藏
IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《我的代码中没有JSON解组输出,但在goplayground中可以正常工作》,聊聊,我们一起来看看吧!
问题内容
在审查了几个问题后,我决定提出我的问题。有几件事我可以说...
- 我插入 json 数据的结构已导出,其字段也已导出。
- 我插入 json 数据的结构是由 protoc 自动生成的。
- 我使用的结构和代码适用于 go 演示 https://goplay.space/#wzws3dsvcr5
我的代码分为几个部分。
定义 queryparm 结构的原型文件消息。
message queryparm { string column_name = 1; string column_type = 2; }
我的结构在 protobuf.pb.go
type queryparm struct { columnname string `protobuf:"bytes,1,opt,name=column_name,json=columnname,proto3" json:"column_name,omitempty"` columntype string `protobuf:"bytes,2,opt,name=column_type,json=columntype,proto3" json:"column_type,omitempty"` wherevalue string `protobuf:"bytes,3,opt,name=where_value,json=wherevalue,proto3" json:"where_value,omitempty"` xxx_nounkeyedliteral struct{} `json:"-"` xxx_unrecognized []byte `json:"-"` xxx_sizecache int32 `json:"-"` }
我的数据在 pg_client.go
type pgdata struct { ... queryparms string `orm:"null"` ... }
以及我在 grpc_client.go 中的函数
func createjobresponse(d *pg.pgdata) (*pb.jobresponse, error) { var qp []*pb.queryparm if d.queryparms == *new(string) { d.queryparms = "[{}]" } fmt.printf("parms: %v\ntype: %t\n", d.queryparms, d.queryparms) if err := json.unmarshal([]byte(d.queryparms), &qp); err != nil { return nil, err } fmt.printf("parms: %v\ntype: %t\n", qp, qp) return &pb.jobresponse{ ... queryparms: qp, ... }, nil }
我在 unmarshal 后收到的输出在我的代码中是空的,并且在演示中包含空的 queryparm 结构指针。 json 字符串显然是预先存在的。
Parms: [{"ColumnName":"message_property_assetId","ColumnType":"string"},{"ColumnName":"id","ColumnType":"string"},{"ColumnName":"message_id","ColumnType":"string"},{"ColumnName":"message_security_tenantId","ColumnType":"string"}] Type: string Parms: [ ] Type: []*proto_export.QueryParm
是否有某种原因导致我的代码和演示的输出应该不同?
结论编辑:
我想说,出于某种奇怪的原因,我使用了与解码 json 不同的结构来编码 json。这导致 json 字段名称不同并导致 json 无法正确解码。
确保使用相同的结构进行编码和解码!
解决方案
两个可行的解决方案:
- 您可以使用:
var qp []interface{}
尝试this:
package main import ( "encoding/json" "fmt" ) type queryparm struct { columnname string `protobuf:"bytes,1,opt,name=column_name,json=columnname,proto3" json:"column_name,omitempty"` columntype string `protobuf:"bytes,2,opt,name=column_type,json=columntype,proto3" json:"column_type,omitempty"` wherevalue string `protobuf:"bytes,3,opt,name=where_value,json=wherevalue,proto3" json:"where_value,omitempty"` xxx_nounkeyedliteral struct{} `json:"-"` xxx_unrecognized []byte `json:"-"` xxx_sizecache int32 `json:"-"` } func main() { jsonstr := `[ {"columnname":"message_property_assetid","columntype":"string"}, {"columnname":"id","columntype":"string"}, {"columnname":"message_id","columntype":"string"}, {"columnname":"message_security_tenantid","columntype":"string"}]` // var qp []queryparm var qp []interface{} if err := json.unmarshal([]byte(jsonstr), &qp); err != nil { return } fmt.println(qp) }
输出:
[map[columnname:message_property_assetid columntype:string] map[columnname:id columntype:string] map[columnname:message_id columntype:string] map[columnname:message_security_tenantid columntype:string]]
- 您的 json 标记中包含
column_name
和column_type
,而不是columnname
和columntype
:json:"column_name,omitempty"
,因此您可以更改输入字符串,例如:
jsonstr := `[ {"column_name":"message_property_assetid","column_type":"string"}, {"column_name":"id","column_type":"string"}, {"column_name":"message_id","column_type":"string"}, {"column_name":"message_security_tenantid","column_type":"string"}]`
尝试this:
package main import ( "encoding/json" "fmt" ) type queryparm struct { columnname string `protobuf:"bytes,1,opt,name=column_name,json=columnname,proto3" json:"column_name,omitempty"` columntype string `protobuf:"bytes,2,opt,name=column_type,json=columntype,proto3" json:"column_type,omitempty"` wherevalue string `protobuf:"bytes,3,opt,name=where_value,json=wherevalue,proto3" json:"where_value,omitempty"` xxx_nounkeyedliteral struct{} `json:"-"` xxx_unrecognized []byte `json:"-"` xxx_sizecache int32 `json:"-"` } func main() { jsonstr := `[ {"column_name":"message_property_assetid","column_type":"string"}, {"column_name":"id","column_type":"string"}, {"column_name":"message_id","column_type":"string"}, {"column_name":"message_security_tenantid","column_type":"string"}]` var qp []queryparm if err := json.unmarshal([]byte(jsonstr), &qp); err != nil { return } fmt.printf("%+v\n", qp) }
输出:
[{ColumnName:message_property_assetId ColumnType:string WhereValue: XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0} {ColumnName:id ColumnType:string WhereValue: XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0} {ColumnName:message_id ColumnType: WhereValue: XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0} {ColumnName:message_security_tenantId ColumnType:string WhereValue: XXX_NoUnkeyedLiteral:{} XXX_unrecognized:[] XXX_sizecache:0}]
- 您可以编写自定义 json 编组和解组。
理论要掌握,实操不能落!以上关于《我的代码中没有JSON解组输出,但在goplayground中可以正常工作》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
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次学习