【问题标题】:Why am I getting '`parse': (<unknown>): mapping values are not allowed in this context' in YAML file in Ruby为什么我在 Ruby 的 YAML 文件中得到 '`parse': (<unknown>): mapping values are not allowed in this context'
【发布时间】:2014-07-16 13:25:45
【问题描述】:

尝试在 Ruby 中打开我的 Yaml 文件时出现上述错误。我已经检查了这个验证器中的 YAML,它已经通过了http://yamllint.com/。我不确定我的 YAML 有什么问题会阻止它打开。有任何想法吗?这是 YAML 文件。我正在尝试使用yml = YAML::load(File.open('servers.yml'))打开文件

---
servers:
  - ps-overture-d01
    location: ps-overture-d01
    tomcat_location: /home/tomcat/tomcat/webapps/report/
    user: tomcat
    menus:
      - Accounts Receivable
        reports:
          - Accounts Receivable Aging Report
            name: AccountsReceivableAgingReport
            location: /public/Common/Reports/Accounts_Receivable_Reports
      - Inventory
        reports:
          - Inventory Master List Report
            name: InventoryMasterListReport
            location: /public/Common/Reports/Inventory_Reports
          - Inventory Totals Report
            name: InventoryTotalsReport
            location: /public/Common/Reports/Inventory_Reports
          - Dealer Purchasing Report
            name: DealerPurchasingReport
            location: /public/Common/Reports/Inventory_Reports
          - DOA Report
            name: DOAReport
            location: /public/Common/Reports/Inventory_Reports
          - Stock Transfers Report
            name: StockTransfersReport
            location: /public/Common/Reports/Inventory_Reports
          - Removed Inventory Report
            name: RemovedInventoryReport
            location: /public/Common/Reports/Inventory_Reports
          - Inventory Order Sheet Report
            name: InventoryOrderSheetReport
            location: /public/Common/Reports/Inventory_Reports
          - Inventory Totals GMROI Report
            name: InventoryTotalsGMROIReport
            location: /public/Common/Reports/Inventory_Reports
          - Master Inventory GMROI Report
            name: MasterInventoryGMROIReport
            location: /public/Common/Reports/Inventory_Reports
          - Dead Stock Report
            name: DeadInventoryReport
            location: /public/Common/Reports/Inventory_Reports
          - Dead Stock Report Details
            name: DeadInventoryReportDetails
            location: /public/Common/Reports/Inventory_Reports
          - Negative Quantity Report
            name: NegativeInventoryQTYReport
            location: /public/Common/Reports/Inventory_Reports

【问题讨论】:

    标签: ruby yaml ruby-2.0


    【解决方案1】:

    您的问题在于没有冒号的行:

      - ps-overture-d01
        location: ps-overture-d01
    

    http://yamllint.com/ 所做的是将它们连接到下一行:

    ? "ps-overture-d01 location"
    : ps-overture-d01
    

    Ruby 的 YAML 库不会这样做,而是会引发错误。我不确定您是否打算将有问题的行连接起来,因此您需要了解如何正确调整 YAML 以提供合理的结构,或者在每一行中添加 id:

      - id: ps-overture-d01
        location: ps-overture-d01
    

    这是您的示例,以及我建议的更改:

    ---
    servers:
      - id: ps-overture-d01
        location: ps-overture-d01
        tomcat_location: /home/tomcat/tomcat/webapps/report/
        user: tomcat
        menus:
          - id: Accounts Receivable
            reports:
              - id: Accounts Receivable Aging Report
                name: AccountsReceivableAgingReport
                location: /public/Common/Reports/Accounts_Receivable_Reports
          - id: Inventory
            reports:
              - id: Inventory Master List Report
                name: InventoryMasterListReport
                location: /public/Common/Reports/Inventory_Reports
              - id: Inventory Totals Report
                name: InventoryTotalsReport
                location: /public/Common/Reports/Inventory_Reports
              - id: Dealer Purchasing Report
                name: DealerPurchasingReport
                location: /public/Common/Reports/Inventory_Reports
              - id: DOA Report
                name: DOAReport
                location: /public/Common/Reports/Inventory_Reports
              - id: Stock Transfers Report
                name: StockTransfersReport
                location: /public/Common/Reports/Inventory_Reports
              - id: Removed Inventory Report
                name: RemovedInventoryReport
                location: /public/Common/Reports/Inventory_Reports
              - id: Inventory Order Sheet Report
                name: InventoryOrderSheetReport
                location: /public/Common/Reports/Inventory_Reports
              - id: Inventory Totals GMROI Report
                name: InventoryTotalsGMROIReport
                location: /public/Common/Reports/Inventory_Reports
              - id: Master Inventory GMROI Report
                name: MasterInventoryGMROIReport
                location: /public/Common/Reports/Inventory_Reports
              - id: Dead Stock Report
                name: DeadInventoryReport
                location: /public/Common/Reports/Inventory_Reports
              - id: Dead Stock Report Details
                name: DeadInventoryReportDetails
                location: /public/Common/Reports/Inventory_Reports
              - id: Negative Quantity Report
                name: NegativeInventoryQTYReport
                location: /public/Common/Reports/Inventory_Reports
    

    【讨论】:

    • 你能不能再举一个例子,让我看看我应该怎么做?谢谢。
    • 谢谢,这很有帮助。你知道我可以在线查看 YAML 的任何好的资源吗?
    猜你喜欢
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多