ColdFusion - 遍历数组中的嵌套结构

ColdFusion - Looping through a nested structure in an array

我有一个 json 从外部返回 API:

{ "data" : { "consignmentDetail" : [ { "consignmentNumber" : "5995600864",
            "parcelNumbers" : [ "15505995600864" ]
          } ],
      "consolidated" : false,
      "shipmentId" : "60764454"
    },
  "error" : null
}

我可以通过反序列化 JSON 并获取 data.ShipmentId 来获取 shipmentId 值。我确实也需要获取 consignmentNumber 的值,但是当我尝试将数组作为集合循环时,出现错误:

"Invalid collection [{parcelNumbers={[15505995603009]},consignmentNumber={5995603009}}]. Must be a valid structure or COM object. "

我目前的代码是:

<cfset consignmentDetailArray = [] >
<cfset consignmentDetailArray = shipmentData.data.consignmentDetail>
<cfset mystruct ={}>

<cfloop collection=#consignmentDetailArray# item="i">
   <cfset myStruct = consignmentDetailArray[i]>
   <cfloop collection="#myStruct#" item="key">
      <cfoutput>#key#: #myStruct[key]#<br /></cfoutput>
   </cfloop>
</cfloop>

知道是什么导致了错误吗?是因为数组中有一个结构体是consignmentDetail的值吗? 如果是这样,关于如何正确循环该结构的任何指示?

我应该补充一点,我是 ColdFusion 的新手,学习曲线仍然很陡峭:) (运行 冷融合 10)

感谢阅读并感谢您提供的任何帮助。

consignmentDetalArray 是一个数组,而不是一个结构,并且您正在使用 cfloop collection=。您想从 1 循环到 len 或使用 cfloop/array 代替。

这是一种修复方法:

<cfloop array="#consignmentDetailArray#" index="myStruct">
  <cfloop collection="#myStruct#" item="key">
    <cfoutput>#key#: #myStruct[key]#<br /></cfoutput>
  </cfloop>
</cfloop>