将 JSON 对象数组转换为 YAML
Converting JSON object array to YAML
我有以下 json 需要转换为 YAML
{
"siteidparam": "lid",
"sites": [
{
"name": "default",
"routingmethod": {
"method": "urlparam",
"siteid": "default",
"urlpath": "default"
}
},
{
"name": "csqcentral",
"routingmethod": {
"method": "urlparam",
"siteid": "capitolsquare",
"urlpath": "csq"
}
}
]
}
我使用了 online JSON to YAML converter 并给出了以下输出,
---
siteidparam: "lid"
sites:
-
name: "default"
routingmethod:
method: "urlparam"
siteid: "default"
urlpath: "default"
-
name: "csqcentral"
routingmethod:
method: "urlparam"
siteid: "capitolsquare"
urlpath: "csq"
当我尝试将生成的相同 YAML 转换回 json from the online service 时,它给出了 "Unable to parse" 异常。
1.) 在 YAML 中表示上述 json 的正确方法是什么?
我想在我的 golang 程序中读取这种 YAML。为此,我正在使用 spf13/viper 库,但我找不到任何能够解码这个数组对象之王的方法。
2.) 如何使用viper读取golang中的这种YAML?示例代码会有所帮助。
代码很难看,但看起来这个库不喜欢对象的嵌套数组。
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\n", viper.GetString("siteidparam"))
sites := viper.Get("sites").([]interface{})
for i, _ := range sites {
site := sites[i].(map[interface{}]interface{})
fmt.Printf("%s\n", site["name"])
routingmethod := site["routingmethod"].(map[interface{}]interface{})
fmt.Printf(" %s\n", routingmethod["method"])
fmt.Printf(" %s\n", routingmethod["siteid"])
fmt.Printf(" %s\n", routingmethod["urlpath"])
}
}
将 YAML 解析为 JSON 的问题在于它在每个项目中都有两个空格。应该是这样的:
---
siteidparam: "lid"
sites:
-
name: "default"
routingmethod:
method: "urlparam"
siteid: "default"
urlpath: "default"
-
name: "csqcentral"
routingmethod:
method: "urlparam"
siteid: "capitolsquare"
urlpath: "csq"
关于您的第二个问题,请在下面找到有关如何实现该问题的简单片段:
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`))
}
我有以下 json 需要转换为 YAML
{
"siteidparam": "lid",
"sites": [
{
"name": "default",
"routingmethod": {
"method": "urlparam",
"siteid": "default",
"urlpath": "default"
}
},
{
"name": "csqcentral",
"routingmethod": {
"method": "urlparam",
"siteid": "capitolsquare",
"urlpath": "csq"
}
}
]
}
我使用了 online JSON to YAML converter 并给出了以下输出,
---
siteidparam: "lid"
sites:
-
name: "default"
routingmethod:
method: "urlparam"
siteid: "default"
urlpath: "default"
-
name: "csqcentral"
routingmethod:
method: "urlparam"
siteid: "capitolsquare"
urlpath: "csq"
当我尝试将生成的相同 YAML 转换回 json from the online service 时,它给出了 "Unable to parse" 异常。
1.) 在 YAML 中表示上述 json 的正确方法是什么?
我想在我的 golang 程序中读取这种 YAML。为此,我正在使用 spf13/viper 库,但我找不到任何能够解码这个数组对象之王的方法。
2.) 如何使用viper读取golang中的这种YAML?示例代码会有所帮助。
代码很难看,但看起来这个库不喜欢对象的嵌套数组。
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\n", viper.GetString("siteidparam"))
sites := viper.Get("sites").([]interface{})
for i, _ := range sites {
site := sites[i].(map[interface{}]interface{})
fmt.Printf("%s\n", site["name"])
routingmethod := site["routingmethod"].(map[interface{}]interface{})
fmt.Printf(" %s\n", routingmethod["method"])
fmt.Printf(" %s\n", routingmethod["siteid"])
fmt.Printf(" %s\n", routingmethod["urlpath"])
}
}
将 YAML 解析为 JSON 的问题在于它在每个项目中都有两个空格。应该是这样的:
---
siteidparam: "lid"
sites:
-
name: "default"
routingmethod:
method: "urlparam"
siteid: "default"
urlpath: "default"
-
name: "csqcentral"
routingmethod:
method: "urlparam"
siteid: "capitolsquare"
urlpath: "csq"
关于您的第二个问题,请在下面找到有关如何实现该问题的简单片段:
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`))
}