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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)