node.js 读取yaml文件

node.js 读取yaml文件

1、安装yamljs包,具体的安转命令为:sudo npm inastall -g yamljs

2、编写名为'manifest.yml'的yaml配置文件

Config:

Srvc: 8008

IPAddress: 12.0.0.1

3、读取配置文件代码:

具体做法是将配置文件中的代码转换为json

YAML = require('yamljs');

// Load yaml file using YAML.load

nativeObject = YAML.load('manifest.yml');

 

jsonstr = JSON.stringify(nativeObject);

jsonTemp = JSON.parse(jsonstr, null);

console.log(jsonTemp)

console.log(jsonstr);

console.log(jsonTemp.Config.Srvc);

 

yamljs参考资料:

https://www.npmjs.com/package/yamljs

yamljs 

Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.

Mainly inspired from Symfony Yaml Component.

How to use

Import yaml.js in your html page:

<script type="text/javascript" src="yaml.js"></script>

Parse yaml string:

nativeObject = YAML.parse(yamlString);

Dump native object into yaml string:

yamlString = YAML.stringify(nativeObject[, inline /* @integer depth to start using inline notation at */[, spaces /* @integer number of spaces to use for indentation */] ]);

Load yaml file:

nativeObject = YAML.load('file.yml');

Load yaml file:

YAML.load('file.yml', function(result)

{

    nativeObject = result;

});

Use with node.js

Install module:

npm install yamljs

Use it:

YAML = require('yamljs');

 

// parse YAML string 

nativeObject = YAML.parse(yamlString);

 

// Generate YAML 

yamlString = YAML.stringify(nativeObject, 4);

 

// Load yaml file using YAML.load 

nativeObject = YAML.load('myfile.yml');

Command line tools

You can enable the command line tools by installing yamljs as a global module:

npm install -g yamljs

Then, two cli commands should become available: yaml2json and json2yaml. They let you convert YAML to JSON and JSON to YAML very easily.

yaml2json

usage: yaml2json [-h] [-v] [-p] [-i INDENTATION] [-s] [-r] [-w] input

 

Positional arguments:

  input                 YAML file or directory containing YAML files.

 

Optional arguments:

  -h, --help            Show this help message and exit.

  -v, --version         Show program's version number and exit.

  -p, --pretty          Output pretty (indented) JSON.

  -i INDENTATION, --indentation INDENTATION

                        Number of space characters used to indent code (use 

                        with --pretty, default: 2).

  -s, --save            Save output inside JSON file(s) with the same name.

  -r, --recursive       If the input is a directory, also find YAML files in 

                        sub-directories recursively.

  -w, --watch           Watch for changes.

json2yaml

usage: json2yaml [-h] [-v] [-d DEPTH] [-i INDENTATION] [-s] [-r] [-w] input

 

Positional arguments:

  input                 JSON file or directory containing JSON files.

 

Optional arguments:

  -h, --help            Show this help message and exit.

  -v, --version         Show program's version number and exit.

  -d DEPTH, --depth DEPTH

                        Set minimum level of depth before generating inline 

                        YAML (default: 2).

  -i INDENTATION, --indentation INDENTATION

                        Number of space characters used to indent code 

                        (default: 2).

  -s, --save            Save output inside YML file(s) with the same name.

  -r, --recursive       If the input is a directory, also find JSON files in 

                        sub-directories recursively.

  -w, --watch           Watch for changes.

examples

# Convert YAML to JSON and output resulting JSON on the console

yaml2json myfile.yml

 

# Store output inside a JSON file

yaml2json myfile.yml > ouput.json

 

# Output "pretty" (indented) JSON

yaml2json myfile.yml --pretty

 

# Save the output inside a file called myfile.json

yaml2json myfile.yml --pretty --save

 

# Watch a full directory and convert any YAML file into its JSON equivalent

yaml2json mydirectory --pretty --save --recursive

 

# Convert JSON to YAML and store output inside a JSON file

json2yaml myfile.json > ouput.yml

 

# Output YAML that will be inlined only after 8 levels of indentation

json2yaml myfile.json --depth 8

 

# Save the output inside a file called myfile.json with 4 spaces for each indentation

json2yaml myfile.json --indentation 4

 

# Watch a full directory and convert any JSON file into its YAML equivalent

json2yaml mydirectory --pretty --save --recursive

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web3.0前沿技术研究者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值