dream8877 2016-02-21 05:39
浏览 915
已采纳

将JSON对象数组转换为YAML

I have the following json which needs to convert to YAML

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

I used online JSON to YAML converter and it gave the following output,

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

when I tried to convert the same generated YAML back to json from the online service, it gives "Unable to parse" exception.

1.) what is the correct way of representing above kind of jsons in YAML?

I want to read this kind of YAML inside my golang program. For that I'm using spf13/viper library, but I couldn't find any method which is able to decode this king of array objects.

2.) How to read this kind of YAML in golang using viper? Sample code would help.

  • 写回答

2条回答 默认 最新

  • dongleiqiao2107 2016-02-21 10:50
    关注

    Code is ugly but looks like this library does not like nested arrays of objects.

    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
    ", viper.GetString("siteidparam"))
    
        sites := viper.Get("sites").([]interface{})
        for i, _ := range sites {
            site := sites[i].(map[interface{}]interface{})
            fmt.Printf("%s
    ", site["name"])
            routingmethod := site["routingmethod"].(map[interface{}]interface{})
            fmt.Printf("  %s
    ", routingmethod["method"])
            fmt.Printf("  %s
    ", routingmethod["siteid"])
            fmt.Printf("  %s
    ", routingmethod["urlpath"])
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 在rhel8中安装qemu-kvm时遇到“cannot initialize crypto:unable to initialize gcrypt“报错”
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 paddle库安装时报错提示需要安装common、dual等库,安装了上面的库以后还是显示报错未安装,要怎么办呀?
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀
  • ¥50 我在一个购物网站的排队系统排队,这个排队到号后重新定向到目标网站进行购物,但是有技术牛通过技术方法直接跳过排队系统进入目标网址购物,有没有什么软件或者脚本可以用
  • ¥15 ios可以实现ymodem-1k协议 1024字节传输吗?