我有一个api电话,可以从第三方获得调查结果。 然后我DeserializeJSON结果,它看起来像这样(在问题结果中的n.b结构已被最小化为空间):
然后我遍历第一级并且可以提取第一级值(例如deviceName id等)但是我似乎无法找到获取questionResults数据的方法。特别是我需要questionText和resultValue变量。 (在问题内部结果总是有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>
但是得到错误“结构不能用作数组”
关于如何获取我所追求的数据的任何建议?
答案 0 :(得分:2)
我认为当你循环遍历结构或数组或几乎任何东西时,脚本语法更容易可视化。
注意:我现在无法访问我的Gists为TryCF保存它。
首先,我设置了我的&#34; JSON&#34;价值:
RewriteRule ^sub$ https://%{HTTP:Host}/newsub [R=301,L]
我创建了一个JSON变量,模拟从http请求返回的内容。然后,我将从中反序列化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"
}
]' ;
这给了我带有结构和其他数组的漂亮数组。
这是我在脚本中找到很多的部分。
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>"
) ;
}
}
如果我可以正确显示我的TryCF.com代码,您会看到此返回:
</cfscript>
注意:我在Lucee中运行此功能。 ACF并不喜欢我的JSON解析,我也没有通过它来找到它不喜欢的角色。它仍然有效。 : - )
答案 1 :(得分:1)
从您的示例代码看,您在第二个循环中引用了相同的初始数组。这看起来不对我。试试这个。
<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>
我将此<cfloop from="1" to="#ArrayLen(recordData[i])#" index="j">
更改为此<cfloop from="1" to="#ArrayLen(recordData[i].questionResults)#" index="j">
<强>更新强>
在回复您的评论时,我通过将此代码#recordData[i][j]#<br>
更改为此代码<cfdump var="#recordData[i].questionResults[j]#">
来更新我的代码示例,因为您不能简单地将复杂值输出为您报告的错误。
您应该可以像questionText
一样引用#recordData[i].questionResults[j].questionText#
数据(以及其他元素)。
答案 2 :(得分:1)
你有一个结构数组,其中一个结构元素是另一个名为questionResults的结构数组。我建议采用这种方法。
<cfloop array="#recordData#" index="ThisStructure">
do the easy stuff
<cfif StructkeyExists(ThisStructure, "questionResults">
loop through that array and process it
closing tags