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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵