Question

I am looking around for some latest material in working with Coldfusion to build a deserialize JSON file into a table . So far I am using the links such as :

[ADobe deserialize JSON][1] I use JSONLINT.com to check for any errors with the JSON file (there were none) I also looked at the example on the link above and found out it throws a CF error shown below![enter image description here][2]

I am just trying to parse data from another local server and I am using the code from the link above as reference. when I copy and paste the code EXACTLY it leads me to the CF error: If you could lead me to any more documentation that can help me conquer this issue? I normally create pages from data in a SQLdatabase through coldfusion with the advantage of CFdump i wanted to try and do it differently

<!--- Get the JSON Feed --->  
<cfhttp url="http://localhost:8500/LearnJS/dataconv.cfm"> 

<!--- JSON data is sometimes distributed as a JavaScript function. 
 The following REReplace functions strip the function wrapper. ---> 
<cfset theData=REReplace(cfhttp.FileContent,  
    "^\s*[[:word:]]*\s*\(\s*","")> 
<cfset theData=REReplace(theData, "\s*\)\s*$", "")> 

<!--- Test to make sure you have JSON data. ---> 
<cfif !IsJSON(theData)> 
<h3>The URL you requested does not provide valid JSON</h3> 
<cfdump var="#theData#"> 

<!--- If the data is in JSON format, deserialize it. ---> 
<cfelse> 

<cfset cfData = DeserializeJSON(theData)> 

<!--- Parse the resulting array or structure and display the data. 
         In this case, the data represents a ColdFusion query that has been 
         serialized by the SerializeJSON function into a JSON structure with 
         two arrays: an array column names, and an array of arrays,  
         where the outer array rows correspond to the query rows, and the 
         inner array entries correspond to the column fields in the row. ---> 
<!--- First, find the positions of the columns in the data array. ---> 


<cfset colList=ArrayLen(cfData.COLUMNS)> 
<cfset cityIdx=ListFind(colList, "City")> 
<cfset tempIdx=ListFind(colList, "Temp")> 
<cfset fcstIdx=ListFind(colList, "Forecasts")> 
<!--- Now iterate through the DATA array and display the data. ---> 
<cfoutput> 
    <cfloop index="i" from="1" to="#ArrayLen(cfData.DATA)#"> 
        <h3>#cfData.DATA[i][cityIdx]#</h3> 
        Current Temperature: #cfData.DATA[i][tempIdx]#<br><br> 
        <b>Forecasts</b><br><br>         
        <cfloop index="j" from="1" to="#ArrayLen(cfData.DATA[i][fcstIdx])#"> 
            <b>Day #j#</b><br> 
            Outlook: #cfData.DATA[i][fcstIdx][j].WEATHER#<br> 
            High: #cfData.DATA[i][fcstIdx][j].HIGH#<br> 
            Low: #cfData.DATA[i][fcstIdx][j].LOW#<br><br> 
        </cfloop> 
    </cfloop> 
</cfoutput> 
</cfif>
Was it helpful?

Solution

ArrayLen() returns an integer, the length of the array. On the following lines...

<cfset colList=ArrayLen(cfData.COLUMNS)> 
<cfset cityIdx=ListFind(colList, "City")>

You set colList to the return of ArrayLen() (an integer) and then try to reference it as a list. That's probably causing an error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top