javascript - 在 ColdFusion 中解析 JSON 时出现问题

标签 javascript json multidimensional-array coldfusion cfml

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

[ADobe 反序列化 JSON][1] 我使用 JSONLINT.com 检查 JSON 文件是否有任何错误(没有错误) 我还查看了上面链接上的示例,发现它引发了如下所示的 CF 错误![在此处输入图像描述][2]

我只是想解析来自另一个本地服务器的数据,并且我使用上面链接中的代码作为引用。 当我准确地复制并粘贴代码时,它会导致我出现 CF 错误: 您是否可以引导我找到更多可以帮助我解决这个问题的文档? 我通常利用 CFdump 的优势,通过 Coldfusion 从 SQL 数据库中的数据创建页面,我想尝试以不同的方式进行操作

<!--- 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() 的返回值(整数),然后尝试将其作为列表引用。这可能会导致错误。

关于javascript - 在 ColdFusion 中解析 JSON 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17178418/

相关文章:

javascript - 当用户开始在 AngularJS 中输入(输入字段长度> 0)时如何检查 ng-pattern?

javascript - 如何在 JavaScript 中为 Laravel 设置 CSRF?

javascript - Angular 6 每 5 秒运行一次代码,但它第一次发出

php - Composer 无法安装 tymon/jwt-auth

C# 使用大型多维(非锯齿状)字节数组执行文件 I/O 的简单方法

c++ - 使用getline输入二维数组

javascript ajax setinterval 延迟

javascript - JQuery ajax 功能不起作用

c# - Xamarin 试图从 web 服务中获取用户,立即崩溃

ruby-on-rails - 从 ruby​​ 多维数组中找到最大的子数组