san*_*mar 6 javascript json yaml
我想将javascript中的json字符串转换为yaml格式。我最近两天在google上尝试操作,但未找到任何解决方案或库。有适用于Java的答案,而不适用于javascript。
假设我有这样的json字符串:
{
"json": [
"fat and rigid"
],
"yaml": [
"skinny and flexible"
],
"object": {
"array": [
{
"null_value": null
},
{
"boolean": true
},
{
"integer": 1
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
转换为yaml:
json:
- fat and rigid
yaml:
- skinny and flexible
object:
array:
- null_value:
- boolean: true
- integer: 1
Run Code Online (Sandbox Code Playgroud)
有一个在线转换器http://www.json2yaml.com/,但是如何在javascript中转换成它。
Puj*_*ava 11
您可以使用yaml NPM 包。
const YAML = require('yaml');
const jsonObject = {
version: "1.0.0",
dependencies: {
yaml: "^1.10.0"
},
package: {
exclude: [ ".idea/**", ".gitignore" ]
}
}
const doc = new YAML.Document();
doc.contents = jsonObject;
console.log(doc.toString());
Run Code Online (Sandbox Code Playgroud)
输出
version: 1.0.0
dependencies:
yaml: ^1.10.0
package:
exclude:
- .idea/**
- .gitignore
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9355 次 |
| 最近记录: |