Arrays 在Go中创建JSON数组
如何使用Go构造和发送JSON数组 例如:Arrays 在Go中创建JSON数组,arrays,json,go,Arrays,Json,Go,如何使用Go构造和发送JSON数组 例如: { myArray: ["one", "two", "three"] } 目前,我能得到的最接近的结果是将JSON作为如下字符串发送到浏览器: { myArrayString: '["once", "two", "three"]' } 这不是我想要实现的。您需要导入“encoding/json”,然后在结构中使用json.Marshal 非常简单,因为@swoogan评论: package main import ( "encoding
{ myArray: ["one", "two", "three"] }
目前,我能得到的最接近的结果是将JSON作为如下字符串发送到浏览器:
{ myArrayString: '["once", "two", "three"]' }
这不是我想要实现的。您需要导入“encoding/json”
,然后在结构中使用json.Marshal
非常简单,因为@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))
}
演示可用