Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将yaml字符串转换为JSON对象_Javascript_Php_Json_Yaml - Fatal编程技术网

Javascript 将yaml字符串转换为JSON对象

Javascript 将yaml字符串转换为JSON对象,javascript,php,json,yaml,Javascript,Php,Json,Yaml,我们有两个独立的代码库,它们使用不同的本地化风格。其中一个代码库使用yaml,另一个使用JSON 现在,我们正在慢慢地用JSON迁移到代码库,但是使用20k yaml字符串和7种不同的语言,手动转换这些内容是一件痛苦的事情。不幸的是,我们在yaml文件中使用的是字符串表示法,而不是对象表示法,因此类似的转换器无法工作 示例yaml cart.title.primary: Cart cart.title.secondary: Buy products cart.dialog.title: Remo

我们有两个独立的代码库,它们使用不同的本地化风格。其中一个代码库使用yaml,另一个使用JSON

现在,我们正在慢慢地用JSON迁移到代码库,但是使用20k yaml字符串和7种不同的语言,手动转换这些内容是一件痛苦的事情。不幸的是,我们在yaml文件中使用的是字符串表示法,而不是对象表示法,因此类似的转换器无法工作

示例yaml

cart.title.primary: Cart
cart.title.secondary: Buy products
cart.dialog.title: Remove product
cart.dialog.text: Are you sure to remove this product?
在转换器中变成这个

{
  "cart.title.primary": "Cart",
  "cart.title.secondary": "Buy products",
  "cart.dialog.title": "Remove product",
  "cart.dialog.text": "Are you sure to remove this product?"
}
但我想要的是,字符串中的每个点实际上都是JSON中的一个对象。因此,理想情况下,我提供的yaml应该是:

{
  "cart": {
    "title": {
      "primary": "Cart",
      "secondary: "Buy Products"
    },
    "dialog": {
      "title": "Remove product",
      "text": "Are you sure to remove this product?"
    }
  }
}

有没有有经验的人做过这样的事情?首选使用PHP或JavaScript。提前谢谢

您可以使用yaml的基本加载组合,这只是假设一个字符串并使用,然后使用中的代码,您可以一次处理每一行来创建新结构

$yaml = 'cart.title.primary: Cart
cart.title.secondary: Buy products
cart.dialog.title: Remove product
cart.dialog.text: Are you sure to remove this product?';

$data = yaml_parse($yaml);

$output = [];
foreach ( $data as $key => $entry ) {
    assignArrayByPath($output, $key, $entry);
}

function assignArrayByPath(&$arr, $path, $value, $separator='.') {
    $keys = explode($separator, $path);

    foreach ($keys as $key) {
        $arr = &$arr[$key];
    }

    $arr = $value;
}

echo json_encode($output, JSON_PRETTY_PRINT);
这给了你

{
    "cart": {
        "title": {
            "primary": "Cart",
            "secondary": "Buy products"
        },
        "dialog": {
            "title": "Remove product",
            "text": "Are you sure to remove this product?"
        }
    }
}
作为Node.js脚本:

#/usr/bin/env节点
常量fs=require('fs')
var file=process.argv[process.argv.length-1]
var json={}
readFileSync(文件,{encoding:'utf8'})
.split(/\r?\n/)
.forEach((行)=>{
[keyPath,value]=line.split(/:*/)
var target=json
var keys=keyPath.split(/\./)
变量计数器=0
key.forEach((key)=>{
柜台++
如果(计数器===keys.length)目标[key]=值
否则{
如果(!(输入目标))目标[输入]={}
目标=目标[键]
}
})
})
log(JSON.stringify(JSON,null,2))
要使用它:

convert.js file.yaml
输出,使用example.yaml作为输入:

{
  "cart": {
    "title": {
      "primary": "Cart",
      "secondary": "Buy products"
    },
    "dialog": {
      "title": "Remove product",
      "text": "Are you sure to remove this product?"
    }
  }
}

可以使用
split
方法从键创建路径数组,然后使用
reduce
方法基于该键数组嵌套属性来构建此嵌套结构

const yaml={
“cart.title.primary”:“cart”,
“购物车.标题.次要”:“购买产品”,
“cart.dialog.title”:“删除产品”,
“cart.dialog.text”:“确实要删除此产品吗?”
}
常量toJson=(数据)=>{
返回Object.keys(数据).reduce((a,k)=>{
k、 拆分('.')。减少((r,e,i,a)=>{
返回r[e]| |(r[e]=(a[i+1]?{}:data[k]))
},a)
归还
}, {})
}
console.log(toJson(yaml))