将JSON对象数组转换为YAML

我有以下json,需要将其转换为YAML </ p>

  {
“ siteidparam”:“ lid”,
“ sites”:[
{
“ name”:“ default”,
“ routingmethod”:{
“ method”:“ urlparam”,\ n“ siteid”:“默认”,
“ urlpath”:“默认”
}
},
{
“ name”:“ csqcentral”,
“ routingmethod”:{
“ method” :“ urlparam”,
“ siteid”:“ capitolsquare”,
“ urlpath”:“ csq”
}
}
]
}
</ code> </ pre>

我使用了在线JSON到YAML转换器,它给出了以下输出,</ p>

< pre> ---
siteidparam:“ lid”

网站:

名称:“ default”
路由方法:
方法:“ urlparam”
siteid:“ default”

urlpath :“默认”

名称:“ csqcentral”
路由方法:
方法:“ urlparam”
siteid:“ capitolsquare”
urlpath:“ csq”
</ code> </ pre>

,当我尝试将同一生成的YAML从在线服务转换回json 时, 它给出了“无法解析”异常。</ p>

1。)在YAML中表示上述json的正确方法是什么? </ p>

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

2。)如何读取此类 使用毒蛇在Golang中的YAML? 示例代码会有所帮助。</ p>
</ div>

展开原文

原文

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.

dongqiao0953
dongqiao0953 您的第二个问题非常模糊。你自己尝试了什么吗?如果是这样,该怎么办?你没有尝试吗?您正在查看什么文档?该自述文件包含示例:github.com/spf13/viper/blob/master/README.md
4 年多之前 回复
douji9184
douji9184 我不知道第二个问题的答案,但是第一个问题的答案是问题中的YAML是表示问题中JSON的正确方法。我不知道为什么链接的站点会出现“无法解析”错误,但是下面的另一个站点解析起来没有问题:yaml-online-parser.appspot.com
4 年多之前 回复

2个回答



代码很丑陋,但看起来该库不喜欢嵌套的对象数组。</ p>

 < 代码>包main 

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

func main(){
viper.SetConfigType(” yaml“ )
var yamlExample = [] byte(`---
siteidparam:“ lid”

网站:

名称:“ default”
路由方法:
方法:“ urlparam”
siteid: “默认”

urlpath:“默认”

名称:“ csqcentral”
路由方法:
方法:“ urlparam”
siteid:“ capitolsquare”
urlpath:“ csq”`)
\ n viper.ReadConfig(bytes.NewReader(yamlExample))

fmt.Printf(“%s
”,viper.GetString(“ siteidparam”))

网站:= viper.Get(“ sites” )。(i的([] interface {})
,_:=范围站点{
site:=站点[i]。(map [interface {}] interface {})
fmt.Printf(“%s

“,site [” name“])
路由方法:= site [” routingmethod“]。(map [interface {}] interface {})
fmt.Printf(“%s
”,routingmethod [“ method”])
fmt.Printf(“%s
”,routingmethod [“ siteid”])
fmt.Printf(“%s
” ,routingmethod [“ urlpath”])
}
}
</ code> </ pre>
</ div>

展开原文

原文

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"])
    }
}



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

  --- 
siteidparam:“ lid”

sites:

名称:“ default”
路由方法:
方法: “ urlparam”
siteid:“默认”

urlpath:“默认”

名称:“ csqcentral”
路由方法:
方法:“ urlparam”
siteid:“ capitolsquare”
urlpath:“ csq“
</ code> </ pre>

关于第二个问题,请在下面的简单代码段中找到有关如何实现该目标的代码:</ p>

  package main  

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

func main(){
viper.SetConfigType(” yaml“)// //或 viper.SetConfigType(“ YAML”)
var yamlExample2 = [] byte(`


siteidparam:“ lid”

sites:

名称:“ default”
路由方法:
方法: “ urlparam”
siteid:“默认”

urlpath:“默认”

名称:“ csqcentral”
路由方法:
方法:“ urlparam”
siteid:“ capitolsquare”
urlpath:“ csq“

viper.ReadConfig(bytes.NewBuffer(yamlExample2))
fmt.P rintln(viper.Get(
sites`))
}
</ code> </ pre>
</ div>

展开原文

原文

The issue with parsing your YAML to JSON is that it has two spaces in each items. It should be like this:

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

About your second question find below a simple snippet about how to achive that:

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`))
}

du5591
du5591 OP的YAML完全有效。 删除前导缩进不会使其更有效。 如果有YAML解析器认为“根”级别的缩进无效,则这些解析器不符合YAML规范。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐