douliao7930 2015-03-24 16:58
浏览 63
已采纳

将JSON对象的字符串编组到JSON数组中

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条回答 默认 最新

  • duanlie3187 2015-03-24 17:17
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致