douzhuan1467 2018-07-31 02:19
浏览 58

使用Golang输入地图数据以进行结构化

I want to put my map data to another map data using Golang. however it has struct type.

Here is my code.

birth := make(map[string]interface{})

birth["docType"] = "registerBirth"
birth["agencyCd"] = string(args[0])
birth["birthYmd"] = string(args[1])
birth["lsTypeNm"] = string(args[2])
birth["monthDiff"] = string(args[3])
birth["nationNm"] = string(args[4])
birth["sexNm"] = string(args[5])
birth["regType"] = string(args[9])
birth["regYmd"] = string(args[10])

I want to put this map data to another map but I want to use struct type.

cattle := make(map[string]interface{})

cattle["docType"] = "information"
cattle["birthInfo"] = struct {
    birth map[string]interface{}
}{
    birth,
}

but, when I get data.. It comes out like this.

{"birthInfo":{},"docType":"information"}

Here Is the example that I want.

"birthInfo": {
        "birthYmd": "2018-07-25",
        "cattleNo": "cow001",
        "docType": "registerBirth",
        "farmNo": "farm001",
        "flatEartagNo": "eartag123123",
        "lsTypeNm": "황소",
        "monthDiff": "2018-07",
        "nationNm": "austria",
        "regType": "직접",
        "regYmd": "20185-07-25",
        "sexNm": "M"
    },
"docType": "information",
...

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duandu9260 2018-08-01 11:54
    关注

    The problem is with this section of code

    cattle["birthInfo"] = struct {
        birth map[string]interface{}
    }{
        birth,
    }
    

    The json package can only marshal values that are exported (public / start with a capital letter).

    Change the code block to this and it will work:

    cattle["birthInfo"] = struct {
        Birth map[string]interface{} // note: capital "Birth", exported
    }{
        birth,
    }
    

    Runnable example with exported field name:

    https://play.golang.org/p/maTKn95AoGM

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算