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

报告相同问题?

悬赏问题

  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。