JavaScript YAML 解析器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4335804/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:57:33  来源:igfitidea点击:

JavaScript YAML Parser

javascripthtmlparsingyaml

提问by Tom

I am looking for a JavaScript YAML parser which converts the YAML into something usable within a HTML page. I've tried this version on Github (https://github.com/visionmedia/js-yaml) but it looks like it only works with node.js

我正在寻找一个 JavaScript YAML 解析器,它将 YAML 转换为 HTML 页面中可用的内容。我已经在 Github ( https://github.com/visionmedia/js-yaml)上尝试过这个版本,但它看起来只适用于 node.js

Which libraries should I be using and is there any sample code to show example usage?

我应该使用哪些库,是否有任何示例代码来显示示例用法?

采纳答案by Vitaly

JS-YAMLparser works in browser. Here is online demo http://nodeca.github.com/js-yaml/. Though, it's primary goal is node.js, and browser version was done just for fun :)

JS-YAML解析器在浏览器中工作。这是在线演示http://nodeca.github.com/js-yaml/。不过,它的主要目标是 node.js,浏览器版本只是为了好玩:)

回答by mjgil

Here is one that I found. Not sure of how much of the spec this meets, but it suited my needs.

这是我找到的一个。不确定这符合多少规范,但它适合我的需求。

https://github.com/jeremyfa/yaml.js

https://github.com/jeremyfa/yaml.js

回答by Diogo Costa

sorry for answering an old post, but I bumped into the same problem as you.

抱歉回答了一个旧帖子,但我遇到了和你一样的问题。

None of the javascript YAML parsers available satisfied my needs so I developed my own: It is available here: http://code.google.com/p/javascript-yaml-parser/

没有可用的 javascript YAML 解析器满足我的需求,所以我开发了自己的:它可以在这里找到:http: //code.google.com/p/javascript-yaml-parser/

Hope it helps somebody :)

希望它可以帮助某人:)

Cumps, Diogo

坎普斯,迪奥戈

回答by nico

js-yamlworks fine in Safari, Chrome and Firefox on OSX. Here is an example?:

js-yaml在 OSX 上的 Safari、Chrome 和 Firefox 中运行良好。这是一个例子?:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Test js-yaml</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="./js-yaml/dist/js-yaml.min.js"></script>
    <script type="text/javascript">

        // YAML string to Javascript object
        var obj = jsyaml.load( 'greeting: hello\nname: world' );
        console.log( obj );

        // YAML file to Javascript object
        $.get( 'https://raw.githubusercontent.com/nodeca/js-yaml/c50f9936bd1e99d64a54d30400e377f4fda401c5/benchmark/samples/document_application2.yaml', function( data ) {
            var obj = jsyaml.load( data );
            console.log( obj );
        });

        // Huge YAML file (7.2 MB) to Javascript object
        $.get( 'https://raw.githubusercontent.com/nodeca/js-yaml/master/benchmark/samples/document_huge.yaml', function( data ) {
            var obj = jsyaml.load( data );
            console.log( obj );
        });

    </script>
</head>
<body>
<h1>Test js-yaml</h1>
<p><a href="https://github.com/nodeca/js-yaml">https://github.com/nodeca/js-yaml</a></p>
</body>
</html>