Coldfusion:api 调用数组和结构上的嵌套循环

标签 coldfusion sendgrid coldfusion-10 cfml coldfusion-11

我的函数调用 SendGrid API。它返回一个数组 + 结构。我正在编写一个函数来返回一个 CFQuery 数据集。

目标
我想将反序列化的数据对象传递给我的函数并获取查询数据集。

这是我的工作代码和输出:

<cfparam name="variables.ddata" default="#structnew()#">
<!--- API Call Code here --->
<cfset arr = DESerializeJSON(returnStruct.Filecontent) />
<cfdump var="#arr#">

CFdump on call

我的代码:
 <cfset arrayit(arrobj= arr) >
 <cfdump var="#variables.ddata#" >
 <cffunction name="arrayit" access="public" returntype="void">
    <cfargument name="arrobj" type="array"  required="yes">
    <cfset var arr=arguments.arrobj />
    <cfloop from="1" to =   "#arrayLen(arr)#" index="i">
       <cfif isValid("string", arr[i])>
          <cfset StructInsert(variables.ddata, i, arr[i]) />
       </cfif>
       <cfif isstruct(arr[i])>
          <cfset structit(structobj = arr[i]) />
       </cfif>
    </cfloop>
</cffunction>

<cffunction name="structit" access="public" returntype="void" output="yes">
   <cfargument name="structobj" type="any" required="yes">
   <cfset  stru = arguments.structobj />
   <cfloop collection="#stru#" item="S"> 
       <cfif isValid("string", stru[S])>
          <cfset StructInsert( variables.ddata, S, stru[S]) />
       </cfif>
       <cfif isarray(stru[S])>
           <cfset arrayit(arrobj = stru[S]) >
       </cfif>
   </cfloop>
</cffunction>

结果:

Result

当我在我的函数中添加这一行时
<cfif isstruct(stru[S])>
    <cfset variables.ddata   =  arrayit(arrobj = stru[S]) />
</cfif>

出现错误:

Element type is undefined in a CFML structure referenced as part of an expression.
The error occurred on line 71.



** 完整代码**
    <cfsavecontent variable="returnStruct.Filecontent">
[{"date":"2016-04-05","stats":[{"type":"category","name":"5","metrics":{"blocks":1,"bounce_drops":0,"bounces":9,"clicks":4,"deferred":1,"delivered":1,"invalid_emails":8,"opens":4,"processed":1,"requests":1,"spam_report_drops":0,"spam_reports":1,"unique_clicks":3,"unique_opens":3,"unsubscribe_drops":0,"unsubscribes":9}}]}]
</cfsavecontent>

<cfset arr = DESerializeJSON(returnStruct.Filecontent) />



<cfloop from="1" to="#arrayLen(arr)#" index="i">
   <cfif isValid("string", arr[i])>
        <cfset StructInsert(variables.ddata, i, arr[i],true ) />
   </cfif>

   <cfif isstruct(arr[i])>
        <cfsavecontent variable="rr">
            <cfdump var="#arr[i]#"  label="Line 48 ERROR" >
        </cfsavecontent>
        <cfset NotifyErrorAdmin(emailBody = "#rr#"  ,emailsubject = "Line 48") />
        <cfset structit(structobj = arr[i]) />
   </cfif>
      <cfif isarray(arr[i])>
        <cfsavecontent variable="rr">
            <cfdump var="#arr[i]#"  label="Line 54 ERROR" >
        </cfsavecontent>
        <cfset NotifyErrorAdmin(emailBody = "#rr#"  ,emailsubject = "Line 54") />
        <cfset arrayit(arrobj = arr[i]) >
    </cfif>
</cfloop>
</cffunction> 

<cffunction name        =   "structit" access="public" returntype="void" output="yes">
<cfargument name        =   "structobj" type="any" required="yes">
<cfset  stru            =   arguments.structobj />

<cfloop collection="#stru#" item="S"> 
    <cfif isValid("string", stru[S])>
         <cfset StructInsert( variables.ddata, S, stru[S],true) />
    </cfif>
    <cfif isarray(stru[S])>
        <cfsavecontent variable="rr">
            <cfdump var="#stru[S]#"  label="Line 86 ERROR" >
        </cfsavecontent>
        <cfset NotifyErrorAdmin(emailBody = "#rr#"  ,emailsubject = "Line 87") />
        <cfset arrayit(arrobj = stru[S]) >
    </cfif>
 <cfif isstruct(stru[S])>
        <cfsavecontent variable="rr">
            <cfdump var="#stru[S]#"  label="Line 97 ERROR" >
        </cfsavecontent>
        <cfset NotifyErrorAdmin(emailBody = "#rr#"  ,emailsubject = "Line 97") />
        <cfset structit(structobj = stru[S]) />
   </cfif>
</cfloop>
</cffunction> 

ERROR Sg3 Sg4 sg5 sg1 err

最佳答案

您的 UDF arrayit接受类型为 array 的参数但是当该条件为真时,则 struct正在通过,错误。
IE。,

<cfif isStruct(stru[S])>

    <!--- This means stru[S] is a struct  --->
    <cfset variables.ddata = arrayit(arrobj = stru[S])>
    <!--- arrObj should be of type 'array' --->
</cfif>

所以,应该是:
<cfif isStruct(stru[S])>
    <cfset variables.ddata = structit(structobj = stru[S])>
</cfif>

但是,这种情况下的错误将与您添加的不同。

此外,
  • StructInsert() 接受一个可选参数 allowoverwrite默认情况下是 false并根据文档:

  • if key exists and allowoverwrite = "False", ColdFusion throws an exception.

    关于Coldfusion:api 调用数组和结构上的嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36510299/

    相关文章:

    ruby-on-rails-3 - ActionMailer 在测试模式下发送真实的电子邮件! - 如何关闭?

    c# - Sendgrid:在一个请求中向不同的收件人发送不同的邮件

    coldfusion - 无法再访问 CFADMIN

    coldfusion - 为什么我的文件没有显示?将 Amazon S3 与 ColdFusion 结合使用

    azure - Azure Function App 中 SendGrid 绑定(bind)的目的

    mysql - CF10 , MYSQL SQL_SELECT_LIMIT=默认值

    coldfusion - 如何在 Coldfusion 10 中获取应用程序数据源名称?

    multithreading - cfthread - 它可以运行多长时间?

    json - 如何为图表中的所有文本设置字体属性?

    ColdFusion 在我的 Ajax 中调试输出