YAML Multi-Line Arrays

后端 未结 5 2025
闹比i
闹比i 2020-12-04 13:35

In YAML, you can easily create multi-line strings. However, I would like the ability to create a multi-line array (mainly for readibility within config files) using the

相关标签:
5条回答
  • 2020-12-04 14:19

    If what you are needing is an array of arrays, you can do this way:

    key:
      - [ 'value11', 'value12', 'value13' ]
      - [ 'value21', 'value22', 'value23' ]
    
    0 讨论(0)
  • 2020-12-04 14:21

    Following Works for me and its good from readability point of view when array element values are small:

    key: [string1, string2, string3, string4, string5, string6]
    

    Note:snakeyaml implementation used

    0 讨论(0)
  • 2020-12-04 14:25

    The following would work:

    myarray: [
      String1, String2, String3,
      String4, String5, String5, String7
    ]
    

    I tested it using the snakeyaml implementation, I am not sure about other implementations though.

    0 讨论(0)
  • 2020-12-04 14:36

    have you tried this?

    -
      name: Jack
      age: 32
    -
      name: Claudia
      age: 25
    

    I get this: [{"name"=>"Jack", "age"=>32}, {"name"=>"Claudia", "age"=>25}] (I use the YAML Ruby class).

    0 讨论(0)
  • 2020-12-04 14:38

    A YAML sequence is an array. So this is the right way to express it:

    key:
      - string1
      - string2      
      - string3
      - string4
      - string5
      - string6
    

    That's identical in meaning to:

    key: ['string1', 'string2', 'string3', 'string4', 'string5', 'string6']
    

    It's also legal to split a single-line array over several lines:

    key: ['string1', 'string2', 'string3', 
      'string4', 'string5', 
      'string6']
    

    and even have multi-line strings in single-line arrays:

    key: ['string1', 'long
      string', 'string3', 'string4', 'string5', 'string6']
    
    0 讨论(0)
提交回复
热议问题