将yaml转换为不带struct的json

huynhminhtuan:
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 {}
icza:

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


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

第一个想法是使用map[string]interface{}

var body map[string]interface{}

但是,如果yaml配置的深度大于1,则此尝试失败,因为此body映射将包含类型为的其他映射map[interface{}]interface{}

问题在于深度是未知的,并且可能有除地图以外的其他值,因此使用map[string]map[string]interface{}效果不好。

一种可行的方法是让yaml解组为type的值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}]}]}

需要注意的一件事:通过Go映射从yaml转换为JSON,您将失去项目的顺序,因为Go映射中的元素(键-值对)未排序。这可能是问题,也可能不是问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章