如何在yaml中定义map [string]对象?

时间:2017-08-09 08:38:15

标签: symfony twig yaml

如何在YAML中定义map[string]个对象?

我有JSON对象

{
    "name": "SampleStore",
    "books": {
        "sample1": {
        "author": "test1",
        "prize": "1221"
       },
        "sample2": {
        "author": "test2",
        "prize": "890"
       }
    }
}

我在YAML中定义了对象:

Types:
    store:
        name:
            type: String
        books:
            Items:
                Referemce: book_details
    book_details:
        author:
            type: String
        prize:
           type: String

但这是列表的语法,我想要书的地图。如何在YAML中定义这些类型的地图?

1 个答案:

答案 0 :(得分:1)

你应该看看这个:

stores:
    -
      name: SampleStore
      books:
       -
        sample1:
         author: test1
         prize: 1221
       -
        sample2:
         author: test2
         prize: 1221

你可以试着用这个来运行:

{%for store in stores %}
 {{ store.name }}

 {{ store.books|length }}
    {%for book  in store.books %}
          {{ book }}  
    {% endfor %} 

{% endfor %}

将产生输出:

SampleStore

2
    Array  
    Array  

你可以玩它at this link

我建议您也使用书名的密钥以便于访问(类似name: sample1)。

希望这个帮助