如何从输出json对象中删除顶级字段名称,但在golang中保留其属性

时间:2019-07-05 14:02:26

标签: json go proto3

我正在实现go API,并将输出作为json对象获取。我得到的输出如下:

{
    "entityId": "5f1",
    "entries": [
        {
            "Fruit": {
                "name": "1cd9",
                "location": "5f1",
                "ID": "7b9",
                "creationDate": "2019-06-26T18:27:10",
                "lastUpdatedDate": "2019-06-26T18:27:28",
                "startDate": "2019-06-26T08:00:00",
                "endDate": "2019-06-26T08:00:00",
                "entryType": 1
            }
        },
        {
           "Fruit": {
                "name": "1c9",
                "location": "51",
                "ID": "79",
                "creationDate": "2019-06-26T18:27:10",
                "lastUpdatedDate": "2019-06-26T18:27:28",
                "startDate": "2019-06-26T08:00:00",
                "endDate": "2019-06-26T08:00:00",
                "entryType": 1
            },
        }]
}

但是,我希望输出如下:

{
    "entityId": "5f1",
    "entries": [
        {
                "name": "1cd9",
                "location": "5f1",
                "ID": "7b9",
                "creationDate": "2019-06-26T18:27:10",
                "lastUpdatedDate": "2019-06-26T18:27:28",
                "startDate": "2019-06-26T08:00:00",
                "endDate": "2019-06-26T08:00:00",
                "entryType": 1

        },
        {

                "name": "1c9",
                "location": "51",
                "ID": "79",
                "creationDate": "2019-06-26T18:27:10",
                "lastUpdatedDate": "2019-06-26T18:27:28",
                "startDate": "2019-06-26T08:00:00",
                "endDate": "2019-06-26T08:00:00",
                "entryType": 1
          }]
}

我在proto3文件中的数据模型是:

message Entry{
  oneof Type {
   Fruit fruit = 1;
   Animal animals = 2;
   Bird birds = 3;

 }
}  

我尝试在原型中使用oneof类型,并在后端go文件中使用嵌套消息传递来填充模型。 Mysql是我的数据库。

var allmt  pb.TypesResponse
for i := 0; i < len(dbEntries); i++ {
mt := &pb.TypesResponse{
            Entries: []*pb.Entry{{
                Type: &pb.Entry_Fruit{
                    dbEntries[i],
                },
            },
            },
        }
   allmt.Entries = append(allmt.Entries, mt.Entries[0])
}

请帮助我实现平面json对象。

0 个答案:

没有答案