我編寫了一個 go 程序,用于將 json 作為對 httpRequest 的響應,但我只能以這種格式創(chuàng)建 json:{ "Country": [ "abc", "def", ], "Population": [ "8388344", "343", ]}內容類型是使用 map[string]string 動態(tài)定義的。有人可以幫我提供以下格式的 json:[ { "Country" :"abc", "Population" :"8388344" }, { "Country" : "def", "Population" :"343" }, ...]請幫幫我..
1 回答

飲歌長嘯
TA貢獻1951條經(jīng)驗 獲得超3個贊
你只需要制作一個結構片。改編自 doc 示例:
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)
這產(chǎn)生:
[
{"Country":"abc","Population":"1234"},
{"Country":"def","Population":"567"}
]
- 1 回答
- 0 關注
- 180 瀏覽
添加回答
舉報
0/150
提交
取消