You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ColdFusion获取数组元素时遇‘尝试解引用标量变量’错误求助

问题解决:NOAA天气Feed过期时间判断报错

错误原因

你的代码存在两个核心问题:

  • 数组元素访问顺序错误:myArrayObj.properties.ends试图直接给数组本身调用properties属性,但myArrayObj是数组而非结构体,ColdFusion因此报错将数组当作结构体解析。正确逻辑应该是先获取数组的第i个元素myArrayObj[i],再访问其内部的properties结构体。
  • ArrayIsDefined用法错误:该函数用于判断数组指定索引是否存在,而ends是结构体的键,并非数组元素,完全不适用这个函数。

修正后的代码方案

方案1:用StructKeyExists判断结构体键(推荐)

<cfloop from="1" to="#ArrayLen(myArrayObj)#" index="i">
    <cfif StructKeyExists(myArrayObj[i].properties, "ends")>
        Expires: #myArrayObj[i].properties.ends#<br>
    </cfif>
</cfloop>

方案2:用IsDefined判断(适配旧版ColdFusion)

<cfloop from="1" to="#ArrayLen(myArrayObj)#" index="i">
    <cfif IsDefined("myArrayObj[i].properties.ends")>
        Expires: #myArrayObj[i].properties.ends#<br>
    </cfif>
</cfloop>

方案3:检查是否为Null(ColdFusion 11+支持)

<cfloop from="1" to="#ArrayLen(myArrayObj)#" index="i">
    <cfif Not IsNull(myArrayObj[i].properties.ends)>
        Expires: #myArrayObj[i].properties.ends#<br>
    </cfif>
</cfloop>

关键提示

  • 必须先定位到数组的具体元素myArrayObj[i],再访问其内部的properties结构体,不能直接对数组本身操作结构体属性。
  • StructKeyExists是判断结构体键是否存在最可靠的方法,优先选用。

内容的提问来源于stack exchange,提问作者Scott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 07:57:36