JavaScript YAML Parser [closed]

2020-01-24 12:31发布

问题:


Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 5 months ago.

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

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

回答1:

JS-YAML parser 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 :)



回答2:

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



回答3:

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/

Hope it helps somebody :)

Cumps, Diogo



回答4:

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

<!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>