I have a json retuned from an external API:
{ "data" : { "consignmentDetail" : [ { "consignmentNumber" : "5995600864",
"parcelNumbers" : [ "15505995600864" ]
} ],
"consolidated" : false,
"shipmentId" : "60764454"
},
"error" : null
}
I can get the shipmentId
value by deserializing the JSON and grabbing data.ShipmentId
. I really need to get the value of consignmentNumber
too, but when I try to loop through the array as a collection I'm getting an error:
"Invalid collection [{parcelNumbers={[15505995603009]},consignmentNumber={5995603009}}]. Must be a valid structure or COM object. "
The code I have so far is:
<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>
Any ideas what's causing the error? Is it because there is a structure in the array that is the value of consignmentDetail
?
If so, any pointers on how to loop through that structure correctly?
I should maybe add that I'm very new to ColdFusion and still on a steep learning curve :) (Running Coldfusion 10)
Thanks for reading and grateful for any help you can give.
consignmentDetalArray is an array, not a structure, and you are using cfloop collection=. You want to either loop from 1 to the len or use cfloop/array instead.
Here is one way to fix it:
<cfloop array="#consignmentDetailArray#" index="myStruct">
<cfloop collection="#myStruct#" item="key">
<cfoutput>#key#: #myStruct[key]#<br /></cfoutput>
</cfloop>
</cfloop>