【问题标题】:Getting values from multiple structs and arrays using Coldfusion使用 Coldfusion 从多个结构和数组中获取值
【发布时间】:2018-04-27 11:05:41
【问题描述】:

我有一个从第 3 方获取调查结果的 api 调用。 然后我对结果进行 DeserializeJSON,它看起来像这样(questionResults 中的 n.b 结构已最小化空间):

然后我循环遍历第一级并可以提取第一级值(例如 deviceName id 等)但是我似乎无法找到获取 questionResults 数据的方法。特别是我需要 questionText 和 resultValue 变量。 (在 questionResults 中总是有 11 个数组结果,每个都有相同的设置结构)

目前我正在使用以下代码:

<cfhttp url="#myurl#" method="POST" result="myresult">
  <cfhttpparam type="URL" name="apiKey" value="#apiKey#">
  <cfhttpparam type="URL" name="SurveyId" value="#SurveyId#">       
</cfhttp>

<cfset recordData=DeserializeJSON(#myresult.filecontent#)>

<cfdump var="#recordData#">

<cfloop from="1" to="#ArrayLen(recordData)#" index="i">
   <cfoutput>
     <strong>Record ID:</strong> #recordData[i].id#<br>

      <cfloop from="1" to="#ArrayLen(recordData[i])#" index="j">
            #recordData[i][j]#<br>
      </cfloop>
   </cfoutput>
   <hr> 
 </cfloop>

但得到错误“结构不能用作数组”

关于如何获取我所追求的数据的任何建议?

【问题讨论】:

  • 这一行&lt;cfloop from="1" to="#ArrayLen(recordData[i])#" index="j"&gt; - recordData[i]is a struct. You can not use the for loop. recordData[i].questionResult`是一个可以循环的数组。
  • 您的 JSON 字符串需要哪些数据点?你能发布一个返回的 JSON 的例子吗?

标签: arrays struct coldfusion cfloop


【解决方案1】:

我认为,当您遍历结构或数组或几乎任何东西时,脚本语法更容易可视化。

注意:我现在无法访问我的 Gists 以将其保存到 TryCF。

首先,我设置我的“JSON”值:

<cfscript>

myresult.filecontent = '[
    {
        deviceIdentifier: "asdf" ,
        deviceName : "localiPad1" ,
        id : "23155736" ,
        otherStuff : "adsfasdfasd" ,
        questionResults : [
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "G8N9" ,
                questionText : "Select User" ,
                questionType : "Text" ,
                resultValue : "Beatriz Pinho"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "2" ,
                questionText : "Question2" ,
                questionType : "Text" ,
                resultValue : "Answer2"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "3" ,
                questionText : "Question3" ,
                questionType : "Text" ,
                resultValue : "Answer3"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "4" ,
                questionText : "Question4" ,
                questionType : "Text" ,
                resultValue : "Answer4"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "5" ,
                questionText : "Question5" ,
                questionType : "Text" ,
                resultValue : "Answer5"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "6" ,
                questionText : "Question6" ,
                questionType : "Text" ,
                resultValue : "Answer6"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "7" ,
                questionText : "Question7" ,
                questionType : "Text" ,
                resultValue : "Answer7"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "8" ,
                questionText : "Question8" ,
                questionType : "Text" ,
                resultValue : "Answer8"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "9" ,
                questionText : "Question9" ,
                questionType : "Text" ,
                resultValue : "Answer9"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "10" ,
                questionText : "Question10" ,
                questionType : "Text" ,
                resultValue : "Answer10"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "11" ,
                questionText : "Question11" ,
                questionType : "Text" ,
                resultValue : "Answer11"
            } 
        ] ,
        moreOtherStuff : "asdfasdfasdfasd"
    } ,
    {
        deviceIdentifier: "fdsa" ,
        deviceName : "localiPad2" ,
        id : "2" ,
        otherStuff : "adsfasdfasd" ,
        questionResults : [
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "G8N9" ,
                questionText : "Select User" ,
                questionType : "Text" ,
                resultValue : "Silent Bob"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "2" ,
                questionText : "Question2" ,
                questionType : "Text" ,
                resultValue : "Answer2"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "3" ,
                questionText : "Question3" ,
                questionType : "Text" ,
                resultValue : "Answer3"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "4" ,
                questionText : "Question4" ,
                questionType : "Text" ,
                resultValue : "Answer4"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "5" ,
                questionText : "Question5" ,
                questionType : "Text" ,
                resultValue : "Answer5"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "6" ,
                questionText : "Question6" ,
                questionType : "Text" ,
                resultValue : "Answer6"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "7" ,
                questionText : "Question7" ,
                questionType : "Text" ,
                resultValue : "Answer7"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "8" ,
                questionText : "Question8" ,
                questionType : "Text" ,
                resultValue : "Answer8"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "9" ,
                questionText : "Question9" ,
                questionType : "Text" ,
                resultValue : "Answer9"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "10" ,
                questionText : "Question10" ,
                questionType : "Text" ,
                resultValue : "Answer10"
            } ,
            {
                answerDate : "2018-04-26T09:25:55:55.0000000" ,
                questionIdentifier : "11" ,
                questionText : "Question11" ,
                questionType : "Text" ,
                resultValue : "Answer11"
            } 
        ] ,
        moreOtherStuff : "asdfasdfasdfasd"
    }
]' ;

我创建了一个 JSON 变量来模拟您从 http 请求返回的内容。然后,我将从中反序列化 JSON。

recordData = deserializeJSON(myresult.filecontent) ;

这给了我一个很好的数组,里面有结构和其他数组。

这是我发现在脚本中要容易得多的部分。

for ( var i IN recordData ) {  // loop through the outer array

    writeOutput("<strong>Record ID:</strong>" & i.id & "<br>") ;

    for ( var j IN i.questionResults ) { // loop through each questionResults
        writeOutput(
            j.questionIdentifier & " - " & 
            j.questionText
            &  " >> " & 
            j.resultValue 
            & " ------- " &
            j.answerDate
            & "<br>"
        ) ;
    }
}

然后关闭我的脚本。

</cfscript>

如果我可以让我的 TryCF.com 代码正确显示,您会看到返回:

Record ID:23155736
G8N9 - Select User >> Beatriz Pinho ------- 2018-04-26T09:25:55:55.0000000
2 - Question2 >> Answer2 ------- 2018-04-26T09:25:55:55.0000000
3 - Question3 >> Answer3 ------- 2018-04-26T09:25:55:55.0000000
4 - Question4 >> Answer4 ------- 2018-04-26T09:25:55:55.0000000
5 - Question5 >> Answer5 ------- 2018-04-26T09:25:55:55.0000000
6 - Question6 >> Answer6 ------- 2018-04-26T09:25:55:55.0000000
7 - Question7 >> Answer7 ------- 2018-04-26T09:25:55:55.0000000
8 - Question8 >> Answer8 ------- 2018-04-26T09:25:55:55.0000000
9 - Question9 >> Answer9 ------- 2018-04-26T09:25:55:55.0000000
10 - Question10 >> Answer10 ------- 2018-04-26T09:25:55:55.0000000
11 - Question11 >> Answer11 ------- 2018-04-26T09:25:55:55.0000000
Record ID:2
G8N9 - Select User >> Silent Bob ------- 2018-04-26T09:25:55:55.0000000
2 - Question2 >> Answer2 ------- 2018-04-26T09:25:55:55.0000000
3 - Question3 >> Answer3 ------- 2018-04-26T09:25:55:55.0000000
4 - Question4 >> Answer4 ------- 2018-04-26T09:25:55:55.0000000
5 - Question5 >> Answer5 ------- 2018-04-26T09:25:55:55.0000000
6 - Question6 >> Answer6 ------- 2018-04-26T09:25:55:55.0000000
7 - Question7 >> Answer7 ------- 2018-04-26T09:25:55:55.0000000
8 - Question8 >> Answer8 ------- 2018-04-26T09:25:55:55.0000000
9 - Question9 >> Answer9 ------- 2018-04-26T09:25:55:55.0000000
10 - Question10 >> Answer10 ------- 2018-04-26T09:25:55:55.0000000
11 - Question11 >> Answer11 ------- 2018-04-26T09:25:55:55.0000000

注意:我在 Lucee 运行它。 ACF 不喜欢我的 JSON 解析,我也没有仔细研究过它不喜欢哪个字符。它仍然有效。 :-)

【讨论】:

    【解决方案2】:

    您有一个结构数组,其中一个结构元素是另一个名为 questionResults 的结构数组。我建议这种方法。

    <cfloop array="#recordData#" index="ThisStructure">
    do the easy stuff 
    <cfif StructkeyExists(ThisStructure, "questionResults">
    loop through that array and process it
    closing tags
    

    【讨论】:

      【解决方案3】:

      从您的示例代码看来,您在第二个循环中引用了相同的初始数组。这在我看来是错误的。试试这个吧。

      <cfloop from="1" to="#ArrayLen(recordData)#" index="i">
          <cfoutput>
              <strong>Record ID:</strong> #recordData[i].id#<br>
      
              <cfloop from="1" to="#ArrayLen(recordData[i].questionResults)#" index="j">
                  <cfdump var="#recordData[i].questionResults[j]#">
              </cfloop>
          </cfoutput>
          <hr> 
      </cfloop>
      

      我把这个&lt;cfloop from="1" to="#ArrayLen(recordData[i])#" index="j"&gt;改成了这个&lt;cfloop from="1" to="#ArrayLen(recordData[i].questionResults)#" index="j"&gt;

      更新

      针对您的评论,我更新了我的代码示例,将代码 #recordData[i][j]#&lt;br&gt; 更改为代码 &lt;cfdump var="#recordData[i].questionResults[j]#"&gt;,因为您不能像您报告的错误所述那样简单地输出复杂值。

      您应该能够像 #recordData[i].questionResults[j].questionText# 这样引用 questionText 数据(和其他元素)。

      【讨论】:

      • 感谢您的回复,我现在收到此错误“无法将复杂对象类型转换为简单值。表达式已请求将变量或中间表达式结果作为简单值。但是,结果无法转换为简单值。简单值是字符串、数字、布尔值和日期/时间值。查询、数组和 COM 对象是复杂值的示例。"
      • 这是因为您引用第二个数组的方式。抱歉,我在原始答案中没有抓住这一点。我已经更新了答案以转储该值。
      • @SamAllen - 除非有理由必须使用 for/to 循环,否则请尝试使用 &lt;cfloop array="..." ...&gt;。对于“数组”循环,index 是数组元素,因此移动的部分更少。使用 imo 更容易。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多