ブラウザからjs-yamlを使用する良い例を見つけるのは難しいようでした。おそらく、node.jsでのパーサーの使用を強調しているためです。
パーサーはhttps://github.com/nodeca/js-yamlにあります。NPMを使用してインストールします
npm install js-yaml
そして、node_modules / js-yaml/binディレクトリからjs-yaml.jsファイルを取得します。
これは、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));
});
});
}());
そして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リポジトリhttps://github.com/nodeca/js-yaml