Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在Go中创建JSON数组_Arrays_Json_Go - Fatal编程技术网

Arrays 在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

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

例如:

{ 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))
}
演示可用