JSON解组不会在我的代码中输出,可在goplayground中使用

在复习了几个问题之后,我决定问我一个问题。我可以说几件事...

  • 我插入json数据的结构被导出,其字段也被导出。
  • 我要插入json数据的结构是由协议自动生成的。
  • 我正在使用的结构和代码可在goplayground 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,name=column_type,json=columnType,proto3" json:"column_type,omitempty"`
    WhereValue           string   `protobuf:"bytes,3,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",{"ColumnName":"message_id",{"ColumnName":"message_security_tenantId","ColumnType":"string"}]
Type: string
Parms: [   ]
Type: []*proto_export.QueryParm

我的代码和Playground的输出应该有所不同吗?

结论编辑:

我想说,出于某种奇怪的原因,我使用与解码JSON不同的结构来编码JSON。这导致JSON字段名称不同,并阻止了JSON正确解码。

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

as516344224 回答:JSON解组不会在我的代码中输出,可在goplayground中使用

两个可行的解决方案:

  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,name=column_type,json=columnType,proto3" json:"column_type,omitempty"`
    WhereValue           string   `protobuf:"bytes,3,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",{"ColumnName":"message_id",{"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_name中没有column_typeColumnNameColumnType,因此您可以更改输入字符串,例如:
json:"column_name,omitempty"

尝试this

    jsonstr := `[
    {"column_name":"message_property_assetId","column_type":"string"},{"column_name":"id",{"column_name":"message_id",{"column_name":"message_security_tenantId","column_type":"string"}]`

输出:

package main

import (
    "encoding/json"
    "fmt"
)

type QueryParm struct {
    ColumnName           string   `protobuf:"bytes,omitempty"`
    XXX_NoUnkeyedLiteral struct{} `json:"-"`
    XXX_unrecognized     []byte   `json:"-"`
    XXX_sizecache        int32    `json:"-"`
}

func main() {
    jsonstr := `[
        {"column_name":"message_property_assetId","column_type":"string"}]`
    var qp []QueryParm
    if err := json.Unmarshal([]byte(jsonstr),&qp); err != nil {
        return
    }
    fmt.Printf("%+v\n",qp)
}

  1. 您可以编写自定义JSON编组和解编。
本文链接:https://www.f2er.com/3126607.html

大家都在问