【发布时间】:2014-04-24 01:31:38
【问题描述】:
请考虑以下代码:
我正在根据我在 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>
我面临的问题:
当 JSON 数据中没有“Lotus Notes”时,我收到错误“元素 Lotus Notes 在作为表达式的一部分引用的 CFML 结构中未定义。”
an 指向行<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Lotus Notes"]#">
当它在 JSON 中有 Lotus Notes 时,它会拾取一些其他元素,我收到错误“元素 Eudora 在作为表达式的一部分引用的 CFML 结构中未定义。”
并指向上述代码中的<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Eudora"]#"> 行
Here 是带有 Lotus Notes 的示例 JSON,日期为 1 月 1 日
和here 没有 Lotus Notes(从 1 月 2 日起)字段,我收到 Eudora 错误
我不明白我错在哪里。请指教
【问题讨论】:
-
检查循环后形成的查询。看起来插入的列和值的数量可能不匹配。
-
顺便说一句,这听起来很像previous question...正如该线程中提到的,
cfif逻辑是有缺陷的。仅仅因为父键“open”存在,并不能保证单个子键“Eudora”,......将永远存在。仅测试父键是不够的。您必须同时测试它们。
标签: coldfusion