2017-08-09 3932 views
0

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

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

我在YAML定义的对象为JSON对象:

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

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

+0

你的YAML中没有顺序(列表)。请说明你认为是一个序列。 – Anthon

回答

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)。

希望这个帮助

相关问题