2017-08-09 19 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)。

希望這個幫助

相關問題