用 Liquid 语言迭代对象数组
Iterate array of objects in Liquid language
我是 Liquid Language 的新手。我有一个用例,我想遍历对象数组并打印一些值。
可能我的语法不正确。
我有一组对象,如下所示。我从其他地方获取这些数据 api
我把它的一部分贴在这里了。
assign htd_product_ids =
[
{
"id": 113608,
"car_name": "Maruti Suzuki Alto K10 VXi",
"mileage": 13479,
"make_year": 2018,
"final_price": 345000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/alto-k10/vxi-kondapur-2018/113608/"
},
{
"id": 113011,
"car_name": "Maruti Suzuki Celerio VXi AMT",
"mileage": 25257,
"make_year": 2016,
"final_price": 413000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/celerio/vxi-amt-kondapur-2016/113011/"
},
{
"id": 112609,
"car_name": "Maruti Suzuki Swift Dzire ZXI",
"mileage": 54600,
"make_year": 2015,
"final_price": 541000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/swift-dzire/zxi-kondapur-2015/112609/"
}
]
我想遍历这个数组并打印 car_name
{% for product in htd_product_ids %}
{{forloop.index}}. {{product.car_name}}
{% endfor %}
forloop.index 工作正常但 product.car_name 没有打印车名。
如果有任何语法错误或其他问题,请指正。
您不能将 json 分配给变量。这是液体的一个已知问题。
使用液体沙箱进行测试(https://masterdatapoc.z6.web.core.windows.net/),您使用的循环是正确的,尽管json可能是错误的:
来源Json:
{"htd_product_ids": [
{
"id": 113608,
"car_name": "Maruti Suzuki Alto K10 VXi",
"mileage": 13479,
"make_year": 2018,
"final_price": 345000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/alto-k10/vxi-kondapur-2018/113608/"
},
{
"id": 113011,
"car_name": "Maruti Suzuki Celerio VXi AMT",
"mileage": 25257,
"make_year": 2016,
"final_price": 413000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/celerio/vxi-amt-kondapur-2016/113011/"
},
{
"id": 112609,
"car_name": "Maruti Suzuki Swift Dzire ZXI",
"mileage": 54600,
"make_year": 2015,
"final_price": 541000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/swift-dzire/zxi-kondapur-2015/112609/"
}
]}
模板:
{% for product in htd_product_ids %}
{{forloop.index}}. {{product.car_name}}
{% endfor %}
输出:
1. Maruti Suzuki Alto K10 VXi
2. Maruti Suzuki Celerio VXi AMT
3. Maruti Suzuki Swift Dzire ZXI
我是 Liquid Language 的新手。我有一个用例,我想遍历对象数组并打印一些值。
可能我的语法不正确。
我有一组对象,如下所示。我从其他地方获取这些数据 api 我把它的一部分贴在这里了。
assign htd_product_ids =
[
{
"id": 113608,
"car_name": "Maruti Suzuki Alto K10 VXi",
"mileage": 13479,
"make_year": 2018,
"final_price": 345000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/alto-k10/vxi-kondapur-2018/113608/"
},
{
"id": 113011,
"car_name": "Maruti Suzuki Celerio VXi AMT",
"mileage": 25257,
"make_year": 2016,
"final_price": 413000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/celerio/vxi-amt-kondapur-2016/113011/"
},
{
"id": 112609,
"car_name": "Maruti Suzuki Swift Dzire ZXI",
"mileage": 54600,
"make_year": 2015,
"final_price": 541000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/swift-dzire/zxi-kondapur-2015/112609/"
}
]
我想遍历这个数组并打印 car_name
{% for product in htd_product_ids %}
{{forloop.index}}. {{product.car_name}}
{% endfor %}
forloop.index 工作正常但 product.car_name 没有打印车名。
如果有任何语法错误或其他问题,请指正。
您不能将 json 分配给变量。这是液体的一个已知问题。
使用液体沙箱进行测试(https://masterdatapoc.z6.web.core.windows.net/),您使用的循环是正确的,尽管json可能是错误的:
来源Json:
{"htd_product_ids": [
{
"id": 113608,
"car_name": "Maruti Suzuki Alto K10 VXi",
"mileage": 13479,
"make_year": 2018,
"final_price": 345000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/alto-k10/vxi-kondapur-2018/113608/"
},
{
"id": 113011,
"car_name": "Maruti Suzuki Celerio VXi AMT",
"mileage": 25257,
"make_year": 2016,
"final_price": 413000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/celerio/vxi-amt-kondapur-2016/113011/"
},
{
"id": 112609,
"car_name": "Maruti Suzuki Swift Dzire ZXI",
"mileage": 54600,
"make_year": 2015,
"final_price": 541000,
"fuel_type": "petrol",
"full_url": "http://www.spinny.com/buy-used-cars/hyderabad/maruti-suzuki/swift-dzire/zxi-kondapur-2015/112609/"
}
]}
模板:
{% for product in htd_product_ids %}
{{forloop.index}}. {{product.car_name}}
{% endfor %}
输出:
1. Maruti Suzuki Alto K10 VXi
2. Maruti Suzuki Celerio VXi AMT
3. Maruti Suzuki Swift Dzire ZXI