2016-02-21 105 views
0

我有以下JSON这需要转换为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,它给出了“无法解析”异常。

1.)在YAML中表示上面那种jsons的正确方法是什么?

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

2.)如何使用viper在golang中读取这种YAML?示例代码将有所帮助。

+2

我不知道答案的第二个问题,但答案首先是您的问题*中的YAML是在您的问题中表示JSON的正确方法。我不知道为什么这个链接网站给出了“无法解析”错误,但是这里有另一个解析它的网站没有问题:http://yaml-online-parser.appspot.com/ –

+0

你的第二个问题是模糊。你有没有尝试过自己?如果是这样,什么?你没有尝试过什么吗?你在看什么文件?本自述文件包含示例:https://github.com/spf13/viper/blob/master/README.md – d1str0

回答

1

代码很丑,但看起来像这个库不喜欢嵌套的对象数组。

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

解析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`)) 
} 
+1

OP的YAML是完全有效的。删除领先的缩进并不能使它更有效。如果有YAML解析器认为“根”级别的缩进无效,那么这些解析器不符合YAML规范。 –