2016-02-21 135 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規範。 –