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

ColdFusion中structFind查找失败 键名正确却无法匹配

ColdFusion结构体键查找失效问题解决

问题表现

  • 反序列化JSON生成结构体后,先获取结构体键列表,再逐个按键查找对应值时,有2个键始终提示不存在
  • 对这两个异常键做字符串比较返回-1,肉眼无法识别键名字符串差异,且两个键名本身提取自对象自身的键列表
  • 代码中replace操作的作用:传输过程中%22字符编码会被中途解码,因此预先将双引号替换为~符号,此处replace是为了还原双引号

复现代码

<cfscript>
    jsonIn = deserializeJSON(replace(result,"~","""","all"));
    typeList = "";
    for(x=1;x lte listLen(structKeyList(jsonIn[1]));x++){
        listAppend(typeList,"VarChar");
    }
    result = queryNew(structKeyList(jsonIn[1]),"");
    for(row in jsonIn) {
        queryAddRow(result);
        writeDump(row);
        for(cell in structKeyList(row)) {
            writeOutput(cell & " found: " & structKeyExists(row,cell) & "<br/>");
            //querySetCell(result, cell, row.find(cell));
        }
    }
    writeDump(result);
</cfscript>

运行诊断输出

结构体查找失效诊断输出截图

根因与解决方案

最初的错误提示存在误导,问题本质不是键不存在,而是这两个键对应的值为undefined,导致查找逻辑误判。

  • 修复方案:将JSON中的null值预先替换为空格后,问题即可解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 19:27:28