登录
首页 >  Golang >  Go问答

我的代码中没有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 无法正确解码。

确保使用相同的结构进行编码和解码!


解决方案


两个可行的解决方案:

  1. 您可以使用:
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]]
  1. 您的 json 标记中包含 column_namecolumn_type,而不是 columnnamecolumntypejson:"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}]
  1. 您可以编写自定义 json 编组和解组。

理论要掌握,实操不能落!以上关于《我的代码中没有JSON解组输出,但在goplayground中可以正常工作》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>