Coldfusion 在不同的情况下得到相同的错误 请考虑下面的代码:

Coldfusion 在不同的情况下得到相同的错误 请考虑下面的代码:,coldfusion,Coldfusion,我根据在url上指定的日期范围获取JSON数据&我将其反序列化为以下内容: <cfhttp url="#urladdress#" method="GET" resolveurl="Yes" throwonerror="Yes"> <cfset cfData=DeserializeJSON(CFHTTP.FileContent)> <cfset arraylen = ArrayLen(cfdata)> 我正在抓取数据并插入MySQL数据库,如下所示

我根据在url上指定的日期范围获取JSON数据&我将其反序列化为以下内容:

<cfhttp url="#urladdress#" method="GET" resolveurl="Yes" throwonerror="Yes">

 <cfset cfData=DeserializeJSON(CFHTTP.FileContent)> 

<cfset arraylen = ArrayLen(cfdata)>

我正在抓取数据并插入MySQL数据库,如下所示:

<cfquery datasource="XX.X.XX.XXX" name="qCoulmnInsert">
    INSERT INTO Mytable
     (aol_int,
     android_Phone_int,
     androidTablet_int,
     apple_mail_int,
     blackberry_int,
     Eudora_int,
     gMail_int,
     Hotmail_int,
     lotus_notes_int,
     other_int,
     other_webmail_int,
     Outlook_int,
     Postbox_int,
     sparrow_int,
     thunderbird_int,
     windowsLiveMail_int,
     yahoo_int,
     iPad_int,
     iphone_int,
     iPod_int,
    <!--- FOR DATES --->
     startdate_dt,
     enddate_dt,
     date_dt)

   VALUES
     <!--- loop through your array --->
     <cfloop from="1" to="#arrayLen(cfData)#" index="i">
     ( <!--- 1 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["aol"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 2 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Android Phone"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 3 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Android Tablet"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 4 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Apple Mail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 5 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Blackberry"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 6 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Eudora"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 7 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["gmail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 8 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["hotmail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 9 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Lotus Notes"]#">
     <cfelse>
     NULL
     </cfif>, 

     <!--- 10 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Other"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 11 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Other Webmail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 12 ---> 
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Outlook"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 13 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Postbox"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 14 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Thunderbird"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 15 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Sparrow"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 16 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Windows Live Mail"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 17 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Yahoo"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 18 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPad"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 19 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPhone"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 20 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPod"]#">
     <cfelse>
     NULL
     </cfif> ,

  <!--- FOR DATES --->
     <cfif structKeyExists(cfData[i], "startdate_dt")>
     <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].startdate_dt#">
     <cfelse>
     NULL
     </cfif>, 

     <cfif structKeyExists(cfData[i], "enddate_dt")>
     <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].enddate_dt#">
     <cfelse>
     NULL
     </cfif>,

     <cfif structKeyExists(cfData[i], "date")>
     <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].date#">
     <cfelse>
     NULL
     </cfif> 

    )

     <cfif i neq arrayLen(cfData)>,</cfif>
     </cfloop>
    </cfquery>

插入到Mytable中
(美国在线),
安卓手机,
安卓利德酒店,
苹果公司,
黑莓国际,
尤多拉·因特,
gMail_int,
Hotmail_int,
lotus_notes_int,
其他,
其他网络邮件,
展望国际,
邮政信箱,
sparrow_int,
雷鸟国际,
windowsLiveMail_int,
雅虎国际,
iPad_int,
iphone_int,
iPod_int,
开始日期,
(完),
日期(日期)
价值观
( 
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
, 
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
,
无效的
, 
无效的
,
无效的
)
,
我面临的问题是:

当JSON数据中没有“LotusNotes”时,我会得到一个错误“LotusNotes元素在作为表达式一部分引用的CFML结构中未定义”

一个指向行

当JSON中有LotusNotes时,它会选择其他元素,我得到错误“element Eudora在作为表达式一部分引用的CFML结构中未定义” 并指向上面代码中的行

带有LotusNotes的JSON示例是1月1日的吗

在没有LotusNotes(从2002年1月起)字段的情况下,我得到了Eudora错误


我不明白我错在哪里。请告知

我不明白您为什么不在参考前检查。我就是这样写的

 <!--- 1 --->
 <cfset data = cfData[i]>
 <cfset isVarDefined = isDefined("variables.data.open.aol")>
 <cfset value = isVarDefined ? data.open.aol : "">
 <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#value#" null="#!isVarDefined#">

 <!--- 2 --->
 <!--- very similar to the code above... --->

只需一次检查是否存在
cfData[i].Open
即可-如果不存在,则所有这些int都将为null,因此只需输出20个null。(或者根本不用担心插入,这取决于这里发生了什么)

此外,您不需要为每一个循环都使用显式硬编码逻辑-只需使用一个循环并使用一段代码检查循环内部是否存在-即:

<cfif StructKeyExists(cfData[i],'open')>

    <cfloop index="Key" array=#['aol','Android Phone','Android Tablet','etc']# >

        ,
        <cfif StructKeyExists( cfData[i].open , Key ) >
            <cfqueryparam value=#cfData[i].open[Key]# cfsqltype="cf_sql_integer" />
        <cfelse>
            <cfqueryparam value="" null=true />
        </cfif>

    </cfloop>

<cfelse>

    #RepeatString( ',null' , 20 )#

</cfif>

,
#RepeatString(',null',20)#

检查循环后形成的查询。看起来插入的列和值的数量可能不匹配。顺便说一句,这听起来很像。。。如该线程所述,
cfif
逻辑有缺陷。仅仅因为父密钥“open”存在,并不能保证单个子密钥“Eudora”。。。将永远存在。仅测试父密钥是不够的。必须同时测试它们。不鼓励使用IsDefined,因为它比其他方法更精确,更容易出现错误结果。虽然在这种情况下,更好的理由是。。它实际上不适用于数组表示法;-)是的。输入字符串必须是一个有效的“变量名”,否则将被定义。