【问题标题】:Translating JSON into Twig Looping Arrays将 JSON 转换为 Twig 循环数组
【发布时间】:2021-09-29 23:54:50
【问题描述】:

我是 JSON 和 Twig 的新手。我能够将基本项目从 JSON 转换为 TWIG,但是当我到达数组部分时,我被卡住了。我尝试过不同类型的事情,但都不是很成功。第一个更复杂:

,所以我还没有对那个做任何事情(这是代码 sn-p 到目前为止试图显示的东西:

Freight List Item: <br>
Quantity: {{Result.FreightInformation.0.0.Item.Quantity}}<br>
Dim Type: {{Result.FreightInformation.0.0.Item.DimType}}<br>
Units: {{Result.FreightInformation.Item.0.0.Units}}<br>
Commodity: {{Result.FreightInformation.I0.0.tem.Commodity}}<br>
Weight: {{Result.FreightInformation.Item.0.0.Weight}}<br>
Length: {{Result.FreightInformation.Item.0.0.Length}}<br>
Width: {{Result.FreightInformation.Item.0.0.Width}}<br>
Height: {{Result.FreightInformation.Item.0.0.Height}}<br>
Class: {{Result.FreightInformation.Item.0.0.Class}}<br>
NMFC: {{Result.FreightInformation.Item.0.0.NMFC}}<br>
Hazmat: {{Result.FreightInformation.Item.0.0.Hazmat}}<br>
<br>

第二个不太复杂(所以我尝试了那个)但是有很多项目已经循环了:

我一直在研究,但现在我需要加快进度,因为等待我完成这项任务的人将考验他们的耐心。

Tracking Information<br>

{% for Result.TrackingInformation in Result.TrackingInformation %}
  {% set counter = ( counter | default(0) ) + 1 %}
  <p>{{ counter ~ ' ). ' Result.TrackingInformation.i.Item }}</p>
Person: {% for i in 0..100 %} {{Result.TrackingInformation.i.Item.Person}} {% endfor %}<br>
Code: {% for i in 0..100 %} {{Result.TrackingInformation.i.Item.Code}} {% endfor %}<br>
Status: {% for i in 0..100 %} {{Result.TrackingInformation.i.Item.Status}} {% endfor %}<br>
Remarks: {% for i in 0..100 %} {{Result.TrackingInformation.i.Item.Remarks}} {% endfor %}<br>
Date: {% for i in 0..100 %} {{Result.TrackingInformation.i.Item.date}} {% endfor %}<br>
Time:{% for i in 0..100 %} {{Result.TrackingInformation.i.Item.time}} {% endfor %}<br>

{% endfor %}

【问题讨论】:

    标签: arrays json loops twig counter


    【解决方案1】:

    我将把这个答案分成两部分,同时反映 sn-p

    片段 1

    我会假设你在这里写的 sn-p 是正确的,但是 twig 没有这个符号并抛出一个奇怪的错误

    键为“0”的数组的键“项”不存在。

    我已经在 GitHub 上打开了一个关于此行为的 ticket,因为我无法解决这个问题,因为我希望得到正确的结果。


    编辑

    所以点表示法不起作用的原因是0.0 部分被解析为浮点数,因此访问这些值的唯一方法是使用数组表示法 - source


    但是,您也可以使用twig 中的数组表示法在此处获得正确的输出

    {{ Result.FreightInformation[0][0].Item.Quantity }}
    

    片段 2

    在这一部分中,您将尝试使用变量从数组中获取正确的索引。但是对于这个 sn-p,您不能使用点符号,因为 twig 会将以下 sn-p 解释为 i 是数组的索引,它不会插入 i到现在的值

    {{Result.TrackingInformation.i.Item.Person}}
    

    简而言之,twig 会将其编译为 $Result['TrackingInformation']['i']['Item']['Person']

    您有两种解决方案来解决此问题

    1. 也使用数组表示法
    {{Result.TrackingInformation[i].Item.Person}}
    
    1. 使用attribute function
    {{ attribute(Result.TrackingInformation, i).Item.Person}}
    

    【讨论】:

    • 感谢您提供非常有用的信息。现在数据正在显示。我正在尝试将它们列出,以便每个跟踪项目在它们自己的组中列出(如 JSON 文件中所示)现在它显示如下:Person:Auto Tracker。自动跟踪器 自动跟踪器 自动跟踪器。自动跟踪器。自动跟踪器。自动跟踪器。自动跟踪器。自动跟踪器。自动跟踪器。自动跟踪器 自动跟踪器。自动跟踪器。自动
    • 现在显示如下:i52.683.myftpupload.com/wp-content/uploads/2021/07/…正确但需要显示如下内容:i52.683.myftpupload.com/wp-content/uploads/2021/07/… View (我以后可以让它好看)
    猜你喜欢
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 2019-03-24
    • 2014-08-26
    • 2021-01-09
    • 2017-04-03
    • 2018-11-01
    相关资源
    最近更新 更多