使用js-yaml这个js库进行json和yaml转换
效果
json转yaml
function toYaml() {
let json = area_json.innerText;
if (json) {
try {
let jsonObj = JSON.parse(json);
area_yaml.innerHTML = hljs.highlight("yaml", jsyaml.dump(jsonObj)).value;
area_json.innerHTML = hljs.highlight("json", json).value;
} catch (e) {
alert(e)
}
}
}
yaml转json
function toJson() {
let yaml = area_yaml.innerText;
if (yaml) {
try {
let json = JSON.stringify(jsyaml.load(yaml), null, 2);
area_json.innerHTML = hljs.highlight("json", json).value;
area_yaml.innerHTML = hljs.highlight("yaml", yaml).value;
} catch (e) {
alert(e)
}
}
}