【问题标题】:Why isn't YAML parsing as expected?为什么 YAML 没有按预期解析?
【发布时间】:2012-12-20 21:26:35
【问题描述】:

我尝试使用 SPYC (https://github.com/mustangostang/spyc/) 解析以下 YAML:

children:
    - root:
        - child one
        - child two:
            - subchild one
            - subchild two
        - child three

我希望它返回如下内容:

["children"]=>array(1){
    ["root"]=>array(3){
        [0]=>string(9) "child one",
        ["child two"]=>array(2){
            [0]=>string(12) "subchild one"
            [1]=>string(12) "subchild two"
        }
        [1]=>string(11) "child three"
    }
}

相反,它会返回类似这样的内容(包含似乎是一堆空且不必要的数组):

array(4) {
  [0]=>
  array(4) {
    ["root"]=>
    array(0) {
    }
    [0]=>
    string(9) "child one"
    [1]=>
    array(3) {
      ["child two"]=>
      array(0) {
      }
      [0]=>
      string(12) "subchild one"
      [1]=>
      string(12) "subchild two"
    }
    [2]=>
    string(11) "child three"
}

我构建 YAML 内容的方式是否有问题,或者 SPYC(解析器)是否存在已知问题?

谢谢!

【问题讨论】:

    标签: php yaml


    【解决方案1】:

    这是生成您正在寻找的结构的 YAML

    children:
        root:
            child one
            child two:
                subchild one
                subchild two
            child three
    

    在您的初始 YAML 中,- 表示您正在启动一个列表/数组。 比如这个 YAML

    items:
        - id: 1
          name: ABC
    
        - id: 2
          name: CDB
    

    生产

    [items] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => ABC
                )
    
            [1] => Array
                (
                    [id] => 2
                    [name] => CDB
                )
    
        )
    

    【讨论】:

    • 我的错误,谢谢你告诉我我的方式错误。现在按预期工作。
    猜你喜欢
    • 2020-04-07
    • 2011-08-19
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2011-08-28
    相关资源
    最近更新 更多