Coldfusion - Displaying array values in a cfloop

69 views Asked by At

I have been trying for days to breakdown a JSON array and it's values. But after countless posts and sites, I still cannot get it properly displayed.

If you look at this feed: https://api.weather.gov/alerts/active?zone=TXC005

You will see there are coordinates in the array. I have been able to get the other values, but the sub arrays are proving to be quite a problem. I'm sure it has to be something very simple.

Here is my code where I loop through:

<cfloop from="1" to="#ArrayLen(myArrayObj)#" index="i">
    <cfoutput>
      <strong>NOAA Feed:</strong> #myArrayObj[i].properties.event#<br></strong>
      Effective: #myArrayObj[i].properties.effective#<br>
      Headline: #myArrayObj[i].properties.headline#<br>
      Area: #myArrayObj[i].properties.areaDesc#<br>
      Type: #myArrayObj[i].properties.messageType#<br>
      Description: #myArrayObj[i].properties.description#<br>
        <cfloop from="1" to="#ArrayLen(myArrayObj[i].geometry.coordinates)#" index="g">
            #myArrayObj[i].geometry.coordinates[g]# 
        </cfloop>
      <cfloop from="1" to="#ArrayLen(myArrayObj[i].properties.geocode.same)#" index="j">
             #myArrayObj[i].properties.geocode.same[j]#<br>
        </cfloop> 
    </cfoutput>
    <hr> 
   </cfloop>

You'll see this section is where it is breaking trying to display the coordinates:

 <cfloop from="1" to="#ArrayLen(myArrayObj[i].geometry.coordinates)#" index="g">
              #myArrayObj[i].geometry.coordinates[g]# 
           </cfloop>

I know what I am missing is super simple.... but I'm just going crazy trying to figure it out. Any help would be greatly appreciated!

1

There are 1 answers

2
snackboy On

When looking at the api object, the coordinate values are in arrays of an array of an array. Referencing those values would look like: coordinates[1][1][1],coordinates[1][1][2],etc.

<cfscript>
    _test = {   geometry: 
                {
                    type: "Polygon",
                    coordinates: [
                        [
                            [
                                -94.620000000000005,
                                31.440000000000001
                            ],
                            [
                                -94.740000000000009,
                                31.450000000000003
                            ],
                            [
                                -94.819999900000013,
                                31.480000000000004
                            ],
                            [
                                -94.819999900000013,
                                31.500000000000004
                            ],
                            [
                                -94.660000000000011,
                                31.460000000000004
                            ],
                            [
                                -94.620000000000005,
                                31.440000000000001
                            ]
                        ]
                    ]
                } 
            }
</cfscript>

<cfdump var=#_test#>
<cfdump var=#_test.geometry#>
<cfdump var=#_test.geometry.coordinates#>
<cfdump var=#_test.geometry.coordinates[1]#>
<cfdump var=#_test.geometry.coordinates[1][1]#>
<cfdump var=#_test.geometry.coordinates[1][1][1]#>
<cfdump var=#_test.geometry.coordinates[1][1][2]#>