将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.
我有以下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>
---
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>
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"])
}
}
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`))
}