将yaml转换为json而不使用struct

时间:2016-11-22 08:36:45

标签: json go yaml

Services: 
-   Orders: 
    -   ID: $save ID1
        SupplierOrderCode: $SupplierOrderCode
    -   ID: $save ID2
        SupplierOrderCode: 111111

我想将这个yaml字符串转换为json,因为源数据是动态的,所以我无法将它映射到结构:

var body interface{}
err := yaml.Unmarshal([]byte(s), &body)

然后我想再次将该接口转换为json字符串:

b, _ := json.Marshal(body)

但是会发生错误:

panic: json: unsupported type: map[interface {}]interface {}

2 个答案:

答案 0 :(得分:17)

前言:我优化并改进了以下解决方案,并在此处将其作为库发布:github.com/icza/dyno。以下convert()功能可用dyno.ConvertMapI2MapS()

问题在于,如果您使用最通用的interface{}类型进行解组,则github.com/go-yaml/yaml包用于解组键值对的默认类型将为map[interface{}]interface{}。< / p>

首先想法是使用map[string]interface{}

var body map[string]interface{}

但是如果yaml配置的深度超过一个,则此尝试会失败,因为此body地图将包含其类型将再次为map[interface{}]interface{}的其他地图。

问题是深度未知,可能还有其他值而不是地图,因此使用map[string]map[string]interface{}并不好。

一种可行的方法是让yaml解组为interface{}类型的值,然后通过结果递归,并将每个遇到的map[interface{}]interface{}转换为map[string]interface{}func convert(i interface{}) interface{} { switch x := i.(type) { case map[interface{}]interface{}: m2 := map[string]interface{}{} for k, v := range x { m2[k.(string)] = convert(v) } return m2 case []interface{}: for i, v := range x { x[i] = convert(v) } } return i } 。必须处理地图和切片。

以下是此转换器功能的一个示例:

func main() {
    fmt.Printf("Input: %s\n", s)
    var body interface{}
    if err := yaml.Unmarshal([]byte(s), &body); err != nil {
        panic(err)
    }

    body = convert(body)

    if b, err := json.Marshal(body); err != nil {
        panic(err)
    } else {
        fmt.Printf("Output: %s\n", b)
    }
}

const s = `Services:
-   Orders:
    -   ID: $save ID1
        SupplierOrderCode: $SupplierOrderCode
    -   ID: $save ID2
        SupplierOrderCode: 111111
`

使用它:

Input: Services:
-   Orders:
    -   ID: $save ID1
        SupplierOrderCode: $SupplierOrderCode
    -   ID: $save ID2
        SupplierOrderCode: 111111

Output: {"Services":[{"Orders":[
    {"ID":"$save ID1","SupplierOrderCode":"$SupplierOrderCode"},
    {"ID":"$save ID2","SupplierOrderCode":111111}]}]}

输出:

{{1}}

有一点需要注意:通过Go地图从yaml切换到JSON,您将失去项目的顺序,因为Go地图中的元素(键值对)不是有序的。这可能是也可能不是问题。

答案 1 :(得分:1)

https://github.com/ghodss/yaml是&#34;围绕go-yaml的包装器,旨在在编组结构时能够更好地处理YAML&#34;。除此之外,它提供了driver.context(name)方法,可以做你想要的。

相关问题