doubiao7410 2016-06-06 12:41
浏览 48
已采纳

如何在golang中建立动态结构?

I have this json file,In this json file there are n number of key as we see A1,B1......................................................................zn, a1,a2.......................................................................an, b1..........................................................................bn etc.

     {
        "_id": "5746992a54c1ae24d53ce651",
        "A1": [
            {
                "a1": [
                    "abc",
                    "def",
                    "ghi"
                ]
            },
            {
                "a2": [
                    "abc",
                    "def",
                    "ghi"
                ]
            },
    .
    .
    ,
             {
                "an": [
                    "abc",
                    "def",
                    "ghi"
                ]
            }
        ],
        "B1": [
            {
                "b1": [
                    "abc",
                    "def",
                    "ghi"
                ]
            },
            {
                "b2": [
                    "abc",
                    "def",
                    "ghi"
                ]
            },
            {
                "bn": [
                    "abc",
                    "def",
                    "ghi"
                ]
            }
        ],
    .
    .
    .
    ,
        "Bn": [
            {
                "b1": [
                    "abc",
                    "def",
                    "ghi"
                ]
            },
            {
                "b2": [
                    "abc",
                    "def",
                    "ghi"
                ]
            },
            {
                "bn": [
                    "abc",
                    "def",
                    "ghi"
                ]
            }
        ]
    }

how to call their structure in golang

type Level1 struct {
    TAGID     bson.ObjectId       `json:"_id" bson:"_id"`
    LEVELTAG2 []Level2            `json:"level2" bson:"level2"`     
}

type LevelTag2 struct{
        LEVEL3 []string           `json:"level3" bson:"level3"`
}

I build this structure in golang is there right way or any other way please help me out

  • 写回答

2条回答 默认 最新

  • dongpixi2648 2016-06-06 13:34
    关注

    When the keys are unknown at compile time, you really can only use map[string]interface{} and then some helper functions to navigate that structure.

    If the keys literally are a1, a2, a3 etc... you can make an actual struct, but it would not be pretty as you have to spell out each and every key.

    In general, when the keys of your documents are part of the data, you can't really create static structures.

    And by "part of the data" I mean:

    {
      "billy":23,
      "tommy":24
    }
    

    vs

    [
      {"name":"billy", "age":23},
      {"name":"tommy", "age":24}
    ]
    

    This second form can be represented as: struct { Name string, Age int }

    while the first one can really only be: map[string]int or map[string]interface{} (if the structure is deep)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 安装 opencv4nodejs 报错
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!