Pregunta

items:
    house:
        - bathroom:
            - toothbrush
            - soap
        - bedroom:
            - bed:
                - pillow
                - sheet
            - closet:
                - clothes:
                    - underwear
                    - socks
    garden:
        - treehouse:
            - toys:
                - nerfgun
                - car
            - window

        - garage:
            - car
            - toolbox:
                - hammer
                - scewdriver
                - pliers
            - lawnmower

Here is another try at this document, it has no compound list (I guess that's how it's called).

items2:
    house:
        - bathroom:
            - toothbrush
            - soap
        - bedroom:
            - bed:
                - pillow
                - sheet
            - closet:
                - clothes:
                    - underwear
                    - socks

Which of those two yaml documents are valid ? I'm still wondering if I can use a list of keyed lists like that (nested list ?):

items:
    - list1:
        -itemA
        -itemB
    - list2:
        -itemC
        -itemD
¿Fue útil?

Solución

You can use this to check if your yaml is ok: yamlint

It's seems ok.

Otros consejos

Yes, it's valid YAML (well, the first two are; in the third, make sure that you have a space after your - in the sequences); but it may not do exactly what you think. In your toy example

items:
    - list1:
        - itemA
        - itemB
    - list2:
        - itemC
        - itemD

the value associated with items is a sequence; and each entry of that sequence is a map with a single key/value pair (for the first entry, the key is list1, and in the second, list2).

What may have confused you in your first real example was how to access each element. Since you tagged this yaml-cpp, here's how you would get, say, the list of the toys in the greenhouse of your first example:

doc["items"]["garden"][0]["treehouse"][0]["toys"];

(Note the [0] before accessing the "treehouse" and "toys" keys.)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top