我可以将动态创建的 json 格式化为 dev express 图表所需的格式吗

Can i format dynamically created json into the required format for dev express charts

我已经编写了一个 go 程序来提供 json 作为对 httpRequest 的响应,但我只能以这种格式创建 json :

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

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

    ]
}

内容类型是使用 map[string]string.Can 动态定义的,请有人帮我按以下格式提供 json:

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

请帮帮我..

你只需要制作一片结构。改编自文档示例:

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)

这会产生:

[
    {"Country":"abc","Population":"1234"},
    {"Country":"def","Population":"567"}
]