arrays - 将 json 对象的字符串编码到 json 数组中

标签 arrays json go

这个问题与this solved one (i.e. similar code有关但这是一个不同的问题。下面代码的输出是一系列的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"
    ]
}

最佳答案

你可以自己连接它们:

var buff bytes.Buffer
buff.WriteByte('[')

for i, j := range jsons {
    if i != 0 {
      buff.WriteByte(',')
    }
    buff.Write([]byte(j))
}
buff.WriteByte(']')

如果您需要进一步清理 json,则可以使用 json.Indentjson.Compact

var output bytes.Buffer
err = json.Indent(&output, buff.Bytes(), "", "  ")
// or json.Compact(&output, buff.Bytes())
if err != nil {
    // something wrong with the json
}

关于arrays - 将 json 对象的字符串编码到 json 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29238600/

相关文章:

go - 如何将 image.RGBA (image.Image) 转换为 image.Paletted?

dns - 如何在 Go 服务器中创建自定义域别名?

javascript - 如何在不知道索引的情况下过滤 3 层深度的数组?得到错误的结果

java - 是什么导致了 java.lang.ArrayIndexOutOfBoundsException 以及如何防止它?

c# - 有没有更好的方法将二维数组写入二进制文件?

arrays - 展平 Lua 中的嵌套列表

http - 如何在 Go 中绑定(bind)一个 http.Client 到一个 IP 地址

javascript - ajax 响应在 jquery 解析器之前反序列化

java - JAX-RS 服务响应返回不带小数位的双字段

javascript - 将平面 json 转换为嵌套