循环通过 ColdFusion 结构

问题描述

谁能帮我遍历一个结构。

<cfset resData2 = ConvertXmlToStruct(gatewayResponse.Filecontent,StructNew())>
<cfset resTran2 = 'resData2.Body.PosResponse.Ver1.0.Transaction.ReportBatchDetail.Details'> 
<cfloop index="idx2" from="1" to=#StructCount(resTran2)# step="1"> 
    <cfdump var="#resTran2#">
</cfloop>

结构如下:

enter image description here

解决方法

您的代码存在一些问题。正如@Miguel-F 指出的那样,您对 restran2 的赋值不应用引号括起来,因为您将赋值变成了文字字符串。其次,您进入 resTran2 的结果是一个结构数组,因此您要用于循环上限的是 ArrayLen() 而不是 StructCount()。最后,因为您的一个键 Ver1.0 中包含一个句点,您需要使用括号表示法而不是点表示法来引用它。

<cfset resData2 = ConvertXmlToStruct(gatewayResponse.Filecontent,StructNew())>
<cfset resTran2 = resData2["Body"]["PosResponse"]["Ver1.0"]["Transaction"]["ReportBatchDetail"]["Details"]> 
<cfloop index="idx2" from="1" to="#ArrayLen(resTran2)#" step="1"> 
    <cfset batchID = resTran2["batchID"][idx2]>
    <cfset batchSeqNbr = resTran2["batchSeqNbr"][idx2]>
    ....
</cfloop>