在回顾了几个问题之后,我决定提出我的问题。我可以说几句话..。
我的代码被分成几个部分。
定义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
}
我收到的post解组输出在代码中为空,并在操场上包含空的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。
确保使用相同的结构进行编码,就像解码一样!
发布于 2019-11-10 21:13:36
两个工作解决方案:
var qp []interface{}
试试这
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]]
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"}]`
试试这
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}]
https://stackoverflow.com/questions/58795386
复制相似问题