栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将yaml转换为不带struct的json

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将yaml转换为不带struct的json

前言:
我优化并改进了以下解决方案,并将其作为库发布在这里:

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配置的深度大于一,则此尝试将失败,因为此

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: %sn", 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: %sn", 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: 111111Output: {"Services":[{"Orders":[    {"ID":"$save ID1","SupplierOrderCode":"$SupplierOrderCode"},    {"ID":"$save ID2","SupplierOrderCode":111111}]}]}

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/471128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号