gpt4 book ai didi

arrays - 在 Go 中创建 JSON 数组

转载 作者:IT王子 更新时间:2023-10-29 01:33:27 24 4
gpt4 key购买 nike

如何使用 Go 构建和发送 JSON 数组?

例如:

{ myArray: ["one", "two", "three"] }

目前我最接近的是将 JSON 作为字符串发送到浏览器,如下所示:

{ myArrayString: '["once", "two", "three"]' } 

这不是我想要实现的目标。

最佳答案

非常直截了当作为@swoogan评论:

package main

import (
"encoding/json"
"fmt"
)

type myJSON struct {
Array []string
}

func main() {
jsondat := &myJSON{Array: []string{"one", "two", "three"}}
encjson, _ := json.Marshal(jsondat)
fmt.Println(string(encjson))
}

可用演示 here .

关于arrays - 在 Go 中创建 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33349695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com