【问题标题】:Converting JSON object array to YAML将 JSON 对象数组转换为 YAML
【发布时间】:2016-06-02 15:29:01
【问题描述】:

我有以下需要转换为 YAML 的 json

{
  "siteidparam": "lid",
  "sites": [
    {
      "name": "default",
      "routingmethod": {
        "method": "urlparam",
        "siteid": "default",
        "urlpath": "default"
      }
    },
    {
      "name": "csqcentral",
      "routingmethod": {
        "method": "urlparam",
        "siteid": "capitolsquare",
        "urlpath": "csq"
      }
    }
  ]
}

我使用了online JSON to YAML converter,它给出了以下输出,

---
  siteidparam: "lid"
  sites: 
    - 
      name: "default"
      routingmethod: 
        method: "urlparam"
        siteid: "default"
        urlpath: "default"
    - 
      name: "csqcentral"
      routingmethod: 
        method: "urlparam"
        siteid: "capitolsquare"
        urlpath: "csq"

当我尝试将生成的相同 YAML 转换回 json from the online service 时,它给出了“无法解析”异常。

1.) 在 YAML 中表示上述类型的 json 的正确方法是什么?

我想在我的 golang 程序中读取这种 YAML。为此,我正在使用 spf13/viper 库,但我找不到任何能够解码这个数组对象之王的方法。

2.) 如何使用 viper 在 golang 中读取这种 YAML?示例代码会有所帮助。

【问题讨论】:

  • 我不知道第二个问题的答案,但第一个问题的答案是您的问题中的 YAML is 是在您的问题中表示 JSON 的正确方法题。我不知道为什么链接的站点出现“无法解析”错误,但这是另一个解析它没有问题的站点:yaml-online-parser.appspot.com
  • 你的第二个问题很模糊。你自己尝试过什么吗?如果是这样,是什么?你什么都没试过吗?你在看什么文档?本自述文件有示例:github.com/spf13/viper/blob/master/README.md

标签: json go yaml viper-go


【解决方案1】:

代码很丑,但看起来这个库不喜欢嵌套的对象数组。

package main

import (
    "bytes"
    "fmt"
    "github.com/spf13/viper"
)

func main() {
    viper.SetConfigType("yaml")
    var yamlExample = []byte(`---
  siteidparam: "lid"
  sites:
    -
      name: "default"
      routingmethod:
        method: "urlparam"
        siteid: "default"
        urlpath: "default"
    -
      name: "csqcentral"
      routingmethod:
        method: "urlparam"
        siteid: "capitolsquare"
        urlpath: "csq"`)

    viper.ReadConfig(bytes.NewReader(yamlExample))

    fmt.Printf("%s\n", viper.GetString("siteidparam"))

    sites := viper.Get("sites").([]interface{})
    for i, _ := range sites {
        site := sites[i].(map[interface{}]interface{})
        fmt.Printf("%s\n", site["name"])
        routingmethod := site["routingmethod"].(map[interface{}]interface{})
        fmt.Printf("  %s\n", routingmethod["method"])
        fmt.Printf("  %s\n", routingmethod["siteid"])
        fmt.Printf("  %s\n", routingmethod["urlpath"])
    }
}

【讨论】:

    【解决方案2】:

    将 YAML 解析为 JSON 的问题在于每个项目中有两个空格。应该是这样的:

    ---
    siteidparam: "lid"
    sites: 
      - 
        name: "default"
        routingmethod: 
          method: "urlparam"
          siteid: "default"
          urlpath: "default"
      - 
        name: "csqcentral"
        routingmethod: 
          method: "urlparam"
          siteid: "capitolsquare"
          urlpath: "csq"
    

    关于您的第二个问题,请在下面找到一个关于如何实现该目标的简单 sn-p:

    package main
    
    import (
        "bytes"
        "fmt"
        "github.com/spf13/viper"
    )
    
    func main() {
        viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")
        var yamlExample2 = []byte(`
    ---
    siteidparam: "lid"
    sites:
      -
        name: "default"
        routingmethod:
          method: "urlparam"
          siteid: "default"
          urlpath: "default"
      -
        name: "csqcentral"
        routingmethod:
          method: "urlparam"
          siteid: "capitolsquare"
          urlpath: "csq"
    `)
        viper.ReadConfig(bytes.NewBuffer(yamlExample2))
        fmt.Println(viper.Get(`sites`))
    }
    

    【讨论】:

    • OP 的 YAML 完全有效。删除前导缩进不会使其更有效。如果有 YAML 解析器认为“根”级别的缩进无效,则这些解析器不符合 YAML 规范。
    猜你喜欢
    • 2017-08-16
    • 2018-11-23
    • 2020-01-20
    • 2012-07-21
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多