douli0531 2015-01-06 13:06
浏览 179
已采纳

我可以将动态创建的json格式化为开发快递图表所需的格式吗

I have written a go program for giving json as a response to the httpRequest,but i am able to create json in this format only :

{
  "Country": [
        "abc",
        "def",

    ],
    "Population": [
        "8388344",
        "343",

    ]
}

The content types are dynamically defined using map[string]string.Can someone please help me out to give the json in the below format:

[
    {
       "Country" :"abc",
       "Population" :"8388344"
    },
    {
        "Country" : "def",
        "Population" :"343"
    },
    ...
]

Please help me out..

  • 写回答

1条回答 默认 最新

  • douci1615 2015-01-06 13:20
    关注

    You just need to make a slice of structs. Adapted from the doc example:

    type Tuple struct {
        Country    string
        Population string
    }
    tuples := []Tuple{
        {Country: "abc", Population: "1234"},
        {Country: "def", Population: "567"},
    }
    
    b, err := json.Marshal(tuples)
    if err != nil {
        fmt.Println("error:", err)
    }
    os.Stdout.Write(b)
    

    This produces:

    [
        {"Country":"abc","Population":"1234"},
        {"Country":"def","Population":"567"}
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的