gpt4 book ai didi

arrays - 使用 Coldfusion 从多个结构和数组中获取值

转载 作者:行者123 更新时间:2023-12-04 00:53:15 25 4
gpt4 key购买 nike

我有一个 api 调用,可以从第 3 方获取调查结果。然后我反序列化 JSON 结果,它看起来像这样(questionResults 中的 n.b 结构已被最小化以节省空间):

enter image description here

然后我循环遍历第一级并可以提取第一级值(例如 deviceName id 等)但是我似乎无法找到获取问题结果数据的方法。特别是我需要 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>

但是得到错误“Struct cannot be used as an array”

关于如何获取我需要的数据有什么建议吗?

最佳答案

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

注意:我现在无法访问我的 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 解析,我还没有仔细研究它不喜欢哪个字符。它仍然有效。 :-)

关于arrays - 使用 Coldfusion 从多个结构和数组中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50061074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com