2010-12-02 103 views
33

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

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

+3

你试过了哪些库?不要让我们猜测。另外,你和他们有什么问题? – 2010-12-02 14:06:40

+1

我最近一直在寻找JavaScript YAML库。经过漫长的搜索,我得出结论,目前没有一个。至少没有支持所有当前功能的合理软件。 js-yaml似乎是“最好的”,但它需要node.js,我不想在我的情况下使用它。另见http://stackoverflow.com/questions/428364/pure-javascript-yaml-library-that-supports-both-dump-and-load – RoToRa 2010-12-02 14:33:40

+0

另见: http://stackoverflow.com/questions/428364/ pure-javascript-yaml-library-that-supports-both-dump-and-load – dreftymac 2012-10-12 00:26:07

回答

24

对于回复旧帖子感到抱歉,但我碰到了和你一样的问题。

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

希望它可以帮助别人:)

Cumps, 迪奥戈

+0

+1很好。我必须尽快尝试! – RoToRa 2011-04-04 10:21:55

3

js-yaml在Safari,Chrome和Firefox在OSX工作正常。这里是一个例子:

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