access json data using swig-template

偶尔善良 提交于 2019-12-23 02:48:05

问题


{
"_id": "1",
"style": "13123",
"category": "dress",
"colors": {
    "Black": {
        "prestock": 50,
        "instock": 60,
        "inactive": 0
    },
    "Blue": {
        "prestock": 30,
        "instock": 0,
        "inactive": 0
    },
    "Red": {
        "prestock": 10,
        "instock": 60,
        "inactive": 0
    }
  }
}

i'm using swig-template to access 'colors' object i need to express each color in this list-format:

  • Black
  • Blue
  • Red
  • how can i access this json?

    ps. i tried other ways but no luck, what i have is {{style_list.colors|sort}} which gives me like this:

    Black, Blue, Red
    

    回答1:


    Use the built-in JavaScript method Object.keys

    <ul>
    {% for color in Object.keys(colors) %}
        <li>{{ color }}</li>
    {% endfor %}
    </ul>
    


    来源:https://stackoverflow.com/questions/19283250/access-json-data-using-swig-template

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!