【问题标题】:How to define map[string]object in yaml?如何在 yaml 中定义 map[string]object?
【发布时间】:2018-01-17 01:05:06
【问题描述】:

如何在 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 中定义这些类型的映射?

【问题讨论】:

  • 您的 YAML 中没有序列(列表)。请指出您认为的序列。

标签: symfony twig yaml


【解决方案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)。

希望有帮助

【讨论】:

    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2020-07-14
    相关资源
    最近更新 更多