我正在寻找一些使用 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/