简体   繁体   中英

Why isn't YAML parsing as expected?

I have the following bit of YAML I am trying to parse with SPYC ( https://github.com/mustangostang/spyc/ ):

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

I would expect it to return something like:

["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"
    }
}

Instead it returns something like this (containing what seems to be a bunch of empty and unnecessary arrays):

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"
}

Is there something wrong with the way I've structured my YAML content perhaps or is there a known problem with SPYC (the parser)?

Thanks!

This is the YAML that will generate the structure you are looking for

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

In your initial YAML, the - indicates that you are starting a list/array. For example, this YAML

items:
    - id: 1
      name: ABC

    - id: 2
      name: CDB

produces

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

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

    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM