使用swig-template访问json数据 [英] access json data using swig-template

查看:146
本文介绍了使用swig-template访问json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
"_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
    }
  }
}

我正在使用swig-template访问颜色"对象 我需要以这种列表格式表示每种颜色:

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

  • 黑色
  • 蓝色
  • 红色
  • 我如何访问此json?

    how can i access this json?

    ps.我尝试了其他方法,但是没有运气, 我所拥有的是{{style_list.colors | sort}},它给了我这样的感觉:

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

    Black, Blue, Red
    

    推荐答案

    使用内置的JavaScript方法Object.keys

    Use the built-in JavaScript method Object.keys

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

    这篇关于使用swig-template访问json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆