这个问题与这个已解决的问题有关(即类似的代码,但它是一个不同的问题。下面代码的输出是一系列 json 对象,但它们加在一起不是 json(至少是我需要的格式) . 即对象不在数组中,并且它们之间没有逗号。如何返回数组中以逗号分隔的对象?
buffer := new(bytes.Buffer)
for _, jsonRawMessage := range sliceOfJsonRawMessages{
if err := json.Compact(buffer, jsonRawMessage); err != nil{
fmt.Println("error")
}
}
output, _ := json.Marshal(buffer.String())
w.Header().Set("Content-Type", contentTypeJSON)
w.Write(output)
输出(2 个不同的 json 对象,但实际上还有更多)
{
"Dir": "/usr/local/go/src/bytes",
"ImportPath": "bytes",
"Name": "bytes",
"Doc": "Package bytes implements functions for the manipulation of byte slices.",
"Target": "/usr/local/go/pkg/darwin_amd64/bytes.a",
"Goroot": true,
"Standard": true,
"Root": "/usr/local/go",
"GoFiles": [
"buffer.go",
"bytes.go",
"bytes_decl.go",
"reader.go"
],
"IgnoredGoFiles": [
"equal_test.go"
],
"Imports": [
"errors",
"io",
"unicode",
"unicode/utf8"
],
"Deps": [
"errors",
"io",
"runtime",
"sync",
"sync/atomic",
"unicode",
"unicode/utf8",
"unsafe"
],
"TestGoFiles": [
"export_test.go"
],
"XTestGoFiles": [
"buffer_test.go",
"bytes_test.go",
"compare_test.go",
"example_test.go",
"reader_test.go"
],
"XTestImports": [
"bytes",
"encoding/base64",
"fmt",
"io",
"io/ioutil",
"math/rand",
"os",
"reflect",
"runtime",
"sort",
"sync",
"testing",
"unicode",
"unicode/utf8"
]
}{
"Dir": "/usr/local/go/src/errors",
"ImportPath": "errors",
"Name": "errors",
"Doc": "Package errors implements functions to manipulate errors.",
"Target": "/usr/local/go/pkg/darwin_amd64/errors.a",
"Goroot": true,
"Standard": true,
"Root": "/usr/local/go",
"GoFiles": [
"errors.go"
],
"Deps": [
"runtime",
"unsafe"
],
慕侠2389804
相关分类