在 Javascript 中从 YAML 文件中读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13785364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading from YAML File in Javascript
提问by nsax91
I am trying to read parameters from a YAML file into Javascript. Is there any good library to do this?
我正在尝试将 YAML 文件中的参数读取到 Javascript 中。有什么好的图书馆可以做到这一点吗?
I've tried these libraries: https://github.com/nodeca/js-yamland http://code.google.com/p/javascript-yaml-parser/
我试过这些库:https: //github.com/nodeca/js-yaml和http://code.google.com/p/javascript-yaml-parser/
but both libraries only have functions that parse YAML when it is given as a string, instead of parsing straight out of a .yml or .yaml file. Are there any parsers that read YAML from a file and convert them to JS objects?
但是这两个库都只有在 YAML 作为字符串给出时解析 YAML 的函数,而不是直接解析 .yml 或 .yaml 文件。是否有任何解析器从文件中读取 YAML 并将它们转换为 JS 对象?
回答by Ray Toal
js-yamldoes. I found this by Googling "node js yaml" because reading from files in JavaScript is done server side with node.js (or something like it), not from a browser.
js-yaml可以。我通过谷歌搜索“node js yaml”发现了这一点,因为从 JavaScript 中读取文件是使用 node.js(或类似的东西)在服务器端完成的,而不是从浏览器。
The README for js-yaml begins
js-yaml 的 README 开始
usage: js-yaml [-h] [-v] [-c] [-j] [-t] file
Positional arguments:
file File with YAML document(s)
用法:js-yaml [-h] [-v] [-c] [-j] [-t] 文件
位置参数:
带有 YAML 文档的文件
That is pretty strong evidence that it doesprocess YAML directly from files.
这是非常有力的证据,它确实直接从文件处理 YAML。
回答by wolfstevent
It seemed to be hard to find a good example of using js-yaml from a browser. Probably because they emphasize the use of the parser in node.js.
似乎很难找到一个从浏览器中使用 js-yaml 的好例子。可能是因为他们强调在 node.js 中使用解析器。
The parser is at https://github.com/nodeca/js-yaml. Use NPM to install it
解析器位于https://github.com/nodeca/js-yaml。使用 NPM 安装
npm install js-yaml
and grab the js-yaml.js file from the node_modules/js-yaml/bin directory.
并从 node_modules/js-yaml/bin 目录中获取 js-yaml.js 文件。
Here is a quick, simple demo that loads a YAML file, uses js-yaml to parse it into objects, and then (for verification) it does a native JSON.stringify to convert the JSON into a string and finally uses jquery $.parseJSON to verify the resulting JSON.
这是一个快速、简单的演示,它加载一个 YAML 文件,使用 js-yaml 将其解析为对象,然后(为了验证)它执行原生 JSON.stringify 将 JSON 转换为字符串,最后使用 jquery $.parseJSON验证生成的 JSON。
(function () {
"use strict";
$(document).ready(function () {
$.get('/common/resources/LessonContentsLv01Ln01.yml')
.done(function (data) {
console.log('File load complete');
console.log(jsyaml.load(data));
var jsonString = JSON.stringify(data);
console.log(jsonString);
console.log($.parseJSON(jsonString));
});
});
}());
And the HTML
和 HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Read YAML</title>
<script src="//code.jquery.com/jquery-2.1.0.js">
</script><script src='/vendor/js/js-yaml.js'>
</script><script src='/test/readYaml.js'>
</script>
</head>
<body>
</body>
</html>
Github repository https://github.com/nodeca/js-yaml
Github 存储库https://github.com/nodeca/js-yaml
回答by cattail
If you want to parse it in web browser, you can load the yaml file you want to parse in scripttag and read it's content using js code, which will provide you string result. And if you want to parse yaml in nodejs environment, you can read file directly and also got string. I don't think this is a problem from directly parse yaml or parse yaml from string.
如果你想在网络浏览器中解析它,你可以加载你想要在script标签中解析的 yaml 文件并使用 js 代码读取它的内容,这将为你提供字符串结果。如果你想在 nodejs 环境中解析 yaml,你可以直接读取文件,也可以得到字符串。我不认为这是直接解析 yaml 或从字符串解析 yaml 的问题。

