Golang相当于`jq --stream -c`

时间:2017-05-29 13:17:32

标签: go

在命令行上使用jq --stream -c,我可以像这样格式化漂亮的JSON:

{
  "object": {
    "something": {
      "key1": 123,
      "key2": 456
    },
    "something_else": {
      "key1": [
        "value1",
        "value2"
      ]
    }
  }
}

进入这个:

[["object","something","key1"],123]
[["object","something","key2"],456]
[["object","something","key2"]]
[["object","something_else","key1",0],"value1"]
[["object","something_else","key1",1],"value2"]
[["object","something_else","key1",1]]
[["object","something_else","key1"]]
[["object","something_else"]]
[["object"]]

我查看了Golang documentation for JSON但找不到任何类似内容。是否有这样的功能我错过了?

更准确地说,我想像这样打印上面的JSON:

object.something.key1=123
object.something.key2=345
object.something_else.key1.0=value1
object.something_else.key1.1=value2

1 个答案:

答案 0 :(得分:0)

  

我查看了JSON的Golang文档但找不到类似的东西。是否有这样的功能我错过了?

不,你没有错过任何东西。当前的JSON库不支持您所说的“开箱即用”。

如果您希望这样做,您需要找到一个能够提供所需灵活性的软件包,或者自己满足json.Marshaler自定义类型。