英文:
Marshalling string of json objects into json array
问题
这个问题与这个已解决的问题(即类似的代码)有关,但是这是一个不同的问题。下面代码的输出是一系列的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"
],
"XTestGoFiles": [
"errors_test.go",
"example_test.go"
],
"XTestImports": [
"errors",
"fmt",
"testing",
"time"
]
}
英文:
This question is related to this solved one (i.e. similar code but it is a different question. The output of the code below is a series of json objects, but taken together they are not json (at least in the format that I need it). i.e. The objects are not in an array, and there is not a comma between them. How can I return comma separated objects in an array?
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)
Output (2 distinct json objects but there's actually a lot more)
{
"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"
],
"XTestGoFiles": [
"errors_test.go",
"example_test.go"
],
"XTestImports": [
"errors",
"fmt",
"testing",
"time"
]
}
答案1
得分: 3
你可以自己拼接它们:
var buff bytes.Buffer
buff.WriteByte('[')
for i, j := range jsons {
if i != 0 {
buff.WriteByte(',')
}
buff.Write([]byte(j))
}
buff.WriteByte(']')
然后,如果需要进一步清理 JSON,你可以使用 json.Indent
或 json.Compact
。
var output bytes.Buffer
err = json.Indent(&output, buff.Bytes(), "", " ")
// 或者 json.Compact(&output, buff.Bytes())
if err != nil {
// JSON 有问题
}
英文:
You can concatenate them yourself:
var buff bytes.Buffer
buff.WriteByte('[')
for i, j := range jsons {
if i != 0 {
buff.WriteByte(',')
}
buff.Write([]byte(j))
}
buff.WriteByte(']')
you can then use json.Indent
or json.Compact
if you need to clean up the json further.
var output bytes.Buffer
err = json.Indent(&output, buff.Bytes(), "", " ")
// or json.Compact(&output, buff.Bytes())
if err != nil {
// something wrong with the json
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论