本文介绍了使用javascript将yaml转换为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了这个Yaml文件:
I got this yaml file:
description:
is_a: AnnotationProperty
labelEN: description
labelPT: descrição
relevance:
is_a: AnnotationProperty
domain: Indicator
labelEN: relevance
labelPT: relevância
title:
is_a: AnnotationProperty
labelPT: título
labelEN: title
range: Literal
,我需要将其转换为json,这样我就可以得到这样的东西:
and I need to convert it to json, so I can get something like this:
{
"description": {
"is_a": "AnnotationProperty",
"labelEN": "description",
"labelPT": "descrição"
},
"relevance": {
"is_a": "AnnotationProperty",
"domain": "Indicator",
"labelEN": "relevance",
"labelPT": "relevância"
},
"title": {
"is_a": "AnnotationProperty",
"labelPT": "título",
"labelEN": "title",
"range": "Literal"
}
}
并将其保存在js变量中...
and save it in a js variable...
那么,我该怎么做?
嘿,请检查下面的链接以获取 YAML到JSON转换器 https://www.yamlonline.com/
Hey, please check link below for YAML to JSON converterhttps://www.yamlonline.com/
推荐答案
您可以使用在节点上运行的简单javascript脚本解决此问题.
You can solve that with a simple javascript script running on node.
- 安装node.js
- 安装
js-yaml
软件包:npm install js-yaml -g
- install node.js
- install the
js-yaml
package:npm install js-yaml -g
然后将该脚本保存到文件中,并使用node.js运行它:
Then save this script into a file, and run it with node.js:
var inputfile = 'input.yml',
outputfile = 'output.json',
yaml = require('js-yaml'),
fs = require('fs'),
obj = yaml.load(fs.readFileSync(inputfile, {encoding: 'utf-8'}));
// this code if you want to save
fs.writeFileSync(outputfile, JSON.stringify(obj, null, 2));
这篇关于使用javascript将yaml转换为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!