[英]Parse YAML response to object - Javascript
Am using swagger-ui, which needs input as in object type.我正在使用 swagger-ui,它需要输入 object 类型。 I was trying
JSON.parse
the YAML response and am getting the below我正在尝试
JSON.parse
YAML 响应并得到以下信息
VM9790:1 Uncaught SyntaxError: Unexpected token s in JSON at position 0
at JSON.parse (<anonymous>)
Please suggest the right way to parse the YAML response to an object请建议正确的方法来解析 YAML 对 object 的响应
Response: https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml响应: https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml
The easiest way to parse a YAML file or response is to use a library for that purpose.解析 YAML 文件或响应的最简单方法是为此目的使用库。 You can try https://github.com/eemeli/yaml , which seems fairly well maintained at the time of writing this answer.
您可以尝试https://github.com/eemeli/yaml ,在撰写此答案时似乎维护得很好。
Install the library in your project:在您的项目中安装库:
npm install yaml
Example usage:示例用法:
import YAML from 'yaml'
// or
const YAML = require('yaml')
YAML.parse(yamlGoesHere)
Use parseDocument.使用解析文档。
package.json package.json
"yaml": "^2.1.3"
import * as yaml from 'yaml';
/*
Other code
*/
const document = yaml.parseDocument(yourYamlFile);
console.log(document.get('yourKey'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.