ColdFusion中的JSON解析问题

Issues parsing JSON in ColdFusion

本文关键字:问题 JSON 中的 ColdFusion      更新时间:2023-09-26

我正在寻找一些使用Coldfusion将反序列化JSON文件构建到表中的最新材料。到目前为止,我正在使用链接,如:

[ADobe反序列化JSON][1]我使用JSONLINT.com来检查JSON文件的任何错误(没有)我还查看了上面链接上的示例,发现它抛出了如下所示的CF错误![2]

我只是想从另一个本地服务器解析数据,我使用的代码从上面的链接作为参考。当我复制和粘贴代码完全导致我CF错误:如果你能给我更多的文档来帮助我解决这个问题吗?我通常通过coldfusion利用CFdump的优势从SQLdatabase中的数据创建页面。我想尝试用不同的方法

<!--- 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>

ArrayLen()返回一个整数,数组的长度。

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

将colList设置为返回ArrayLen()(一个整数),然后尝试将其作为列表引用。