我如何 return 从 JSON 文件到网站的所有值?

How do I return all values from JSON file to website?

我正在做一个小型网上商店项目,但 运行 遇到了问题。我试图将 JSON 文件中的每个产品打印到我的 HTML,但它只打印了一个。我让它在终端上工作,但在使用 Jinja 时却不行,所以我猜那里有问题。

这里是 python 函数:

@app.route('/testing')
def testing():
    with open('clothes/hoodies.json') as f:
        data = json.load(f)
    
    for name in data['hoodies']:
        product_name = name['product-name'].splitlines()

    for price in data['hoodies']:
        product_price = price['price'].splitlines()

    for color in data['hoodies']:
        product_color = color['color'].splitlines()
    
    for image in data['hoodies']:
        img = image['image']
    return render_template('test.html', product_name=product_name, product_color=product_color, product_price=product_price, img=img)

这是我的 HTML:

{% for name in product_name %}
<h1>{{ name }}</h1>
{% endfor %}
<!---->
{% for price in product_price %}
<h1>{{ price }}</h1>
{% endfor %}
<!---->
{% for color in product_color %}
<h1>{{ color }}</h1>
{% endfor %}
<!---->
{% for image in img %}
<img src="{{ image }}" alt="image" />
{% endfor %}

这是 JSON 文件的一小部分:

"hoodies": [
    {
      "product-name": "Reebok Classics fleece hoodie in green",
      "price": "54.95",
      "color": "Green",
      "image": [
        "link",
        "link",
        "link",
      ]
    },
    {
      "product-name": "COLLUSION Unisex hoodie in tie dye with print",
      "price": "25.00",
      "color": "Multi",
      "image": [
        "link",
        "link",
        "link",
      ]
    },
    {
      "product-name": "ASOS DESIGN organic muscle hoodie with split hem in brown",
      "price": "20.00",
      "color": "Tobacco brown",
      "image": [
        "link",
        "link",
        "link",
      ]
    },

你目前有 运行 个循环,但实际上你并没有将每个项目保存在某个结构中,因此它只保留每个循环的最后一个项目。试试这个(对每个循环做同样的事情):

改变这个:

for name in data['hoodies']:
        product_name = name['product-name'].splitlines()

对此:

product_name=[name['product-name'].splitlines() for name in data['hoodies']]

完整代码:

@app.route('/testing')
def testing():
    with open('clothes/hoodies.json') as f:
        data = json.load(f)
    
    product_name=[i['product-name'].splitlines() for i in data['hoodies']]

    product_price=[i['price'].splitlines() for i in data['hoodies']]

    product_color=[i['color'].splitlines() for i in data['hoodies']]

    img=[i['image'] for i in data['hoodies']]

    return render_template('test.html', product_name=product_name, product_color=product_color, product_price=product_price, img=img)

您正在重新定义 product_nameproduct_priceproduct_colorimg 的值,而不是创建列表。因此,这些变量的值只会是循环中的最后一项。你可以按照@loaTzimas 展示的那样做,因为它需要更少的代码,或者如果你不熟悉列表理解,你可以这样做:

product_name = []
product_price = []
product_color = []
img = []

for name in data['hoodies']:
    product_name.append(name['product-name'].splitlines())

for price in data['hoodies']:
    product_price.append(price['price'].splitlines())

for color in data['hoodies']:
    product_color.append(color['color'].splitlines())

for image in data['hoodies']:
    img.append(image['image'])