如何在 CFML 循环内的变量中完成嵌套循环迭代

发布于 2025-01-12 20:07:05 字数 1905 浏览 1 评论 0原文

我正在尝试循环访问 CFML 中的 json 响应,并且需要执行一次调用来获取页数,然后调用每个连续的页面,然后循环访问项目以获取数据。这样做时,我有一个需要嵌套变量的嵌套循环。因此,例如,我的初始循环将导致:

#jsonarray.items.1.id#
#jsonarray.items.2.id#
#jsonarray.items.3.id#
#jsonarray.items.4.id#
and so on.

因此,在我的内部循环中,我尝试通过执行另一个循环来替换该数字,如下所示:

  <cfloop from="1" to="50" index="thisBatchItem">
  </cfloop> 

但是,我必须将索引嵌套在我的变量中,当然,不起作用。

有人可以指出我在这里缺少什么吗?

这是我正在使用的代码,您会看到我遇到问题的明显地方。我放入 #jsonarray.items.#thisBatchItem#.id# 只是为了显示我试图在哪里实现这一点。我知道这行不通。

<cfhttp method="GET" result="httpResp" url="https://example.com/api?filter[batch.date][gte]=2022-02-01&filter[batch.date][lte]=2022-03-07&page=1&per-page=50" throwonerror="false">
    <cfhttpparam type="header" name="Authorization" value="Bearer XXXXXXXXXXXXXXXXXXXXXXXXXX">
    <cfhttpparam type="header" name="Content-type" value="application/json">
</cfhttp>            

<cfoutput>
    <cfloop from="1" to="#pageCount#" index="thisPage">

        <cfhttp method="GET" result="httpResp" url="https://example.com/api?filter[batch.date][gte]=2022-02-01&filter[batch.date][lte]=2022-03-07&page=#thisPage#&per-page=50" throwonerror="false">
            <cfhttpparam type="header" name="Authorization" value="Bearer XXXXXXXXXXXXXXXXXXXXXXXXXX">
            <cfhttpparam type="header" name="Content-type" value="application/json">
        </cfhttp>            
        <cfset jsonarray = deserializeJson(httpResp.fileContent)>
        <cfloop from="1" to="50" index="thisBatchItem">
            <cfif StructKeyExists(jsonarray,"#jsonarray.items.#thisBatchItem#.id#")>
                #jsonarray.items.[#thisBatchItem#].id#<br>
            </cfif>
        </cfloop>
    </cfloop>
</cfoutput>

I am trying to loops through a json response in CFML, and need to do one call to get the number of pages, then a call for each successive page, then loop through items to get my data. In doing so I have a nested loop that requires a nested variable. So, for example, my initial loop will result in:

#jsonarray.items.1.id#
#jsonarray.items.2.id#
#jsonarray.items.3.id#
#jsonarray.items.4.id#
and so on.

So in my inside loop, I am trying to replace that number by doing another loop like this:

  <cfloop from="1" to="50" index="thisBatchItem">
  </cfloop> 

But then, I would have to nest the index inside my variable, and that of course does not work.

Can someone point to what I am missing here?

Here is the code I am working with, and you will see the obvious places where I have the problem. I put in #jsonarray.items.#thisBatchItem#.id# just to show where I am trying to make this happen. I know it does not work.

<cfhttp method="GET" result="httpResp" url="https://example.com/api?filter[batch.date][gte]=2022-02-01&filter[batch.date][lte]=2022-03-07&page=1&per-page=50" throwonerror="false">
    <cfhttpparam type="header" name="Authorization" value="Bearer XXXXXXXXXXXXXXXXXXXXXXXXXX">
    <cfhttpparam type="header" name="Content-type" value="application/json">
</cfhttp>            

<cfoutput>
    <cfloop from="1" to="#pageCount#" index="thisPage">

        <cfhttp method="GET" result="httpResp" url="https://example.com/api?filter[batch.date][gte]=2022-02-01&filter[batch.date][lte]=2022-03-07&page=#thisPage#&per-page=50" throwonerror="false">
            <cfhttpparam type="header" name="Authorization" value="Bearer XXXXXXXXXXXXXXXXXXXXXXXXXX">
            <cfhttpparam type="header" name="Content-type" value="application/json">
        </cfhttp>            
        <cfset jsonarray = deserializeJson(httpResp.fileContent)>
        <cfloop from="1" to="50" index="thisBatchItem">
            <cfif StructKeyExists(jsonarray,"#jsonarray.items.#thisBatchItem#.id#")>
                #jsonarray.items.[#thisBatchItem#].id#<br>
            </cfif>
        </cfloop>
    </cfloop>
</cfoutput>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾听心声的旋律 2025-01-19 20:07:05

由于 items 是一个结构数组,因此使用“数组”循环更简单、更清晰。

如果某些结构键是可选的,请尝试使用安全导航运算符 <代码>?.。在下面的示例中,当代码使用不存在的键名(如“lookMaNoErrors”)时,它可以防止发生错误?

<cfset jsonResponse = deserializeJson(httpResp.fileContent)>
<cfloop array="#jsonResponse.items#" index="currentItem">
    id #currentItem.id#<br> 
    lookMaNoErrors #currentItem?.lookMaNoErrors#<br>
</cfloop>

更新:

要回答原始问题,您需要使用结构符号。另外,不要对循环的上限进行硬编码,而是使用数组长度:

<cfloop from="1" to="#arrayLen(jsonResponse.items)#" index="itemIndex">
    id #jsonResponse.items[itemIndex].id#            
    
</cfloop>

demo trycf.com

Since items is an array of structures, it's simpler and cleaner to use an "array" loop.

If some of the structure keys are optional, try using the safe navigation operator ?.. In the example below, it prevents an error from occurring when the code uses a non-existent key name like "lookMaNoErrors"?

<cfset jsonResponse = deserializeJson(httpResp.fileContent)>
<cfloop array="#jsonResponse.items#" index="currentItem">
    id #currentItem.id#<br> 
    lookMaNoErrors #currentItem?.lookMaNoErrors#<br>
</cfloop>

Update:

To answer the original question, you need to use structure notation. Also, instead of hard coding the upper limit of the loop, use the array length instead:

<cfloop from="1" to="#arrayLen(jsonResponse.items)#" index="itemIndex">
    id #jsonResponse.items[itemIndex].id#            
    
</cfloop>

demo trycf.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文