我有以下结构,我正在尝试做的是为其中的不同键设置变量(personaID,personaName,userClubList),然后为userClubList数组中的值设置变量但不知道如何沿着结构向下工作,找出它有哪些键,然后键的值是什么。
CF Struct http://s21.postimage.org/ndg1t9hvb/struct.jpg!
有人可以帮帮我吗?
答案 0 :(得分:2)
实际上,您已经能够使用此结构,并且不需要任何映射到其他变量。通过使用点符号userAccountInfo.personas
或方括号userAccountInfo["personas"]
来访问结构。您可以通过链接访问userAccountInfo.personas[1].userClubList[1].clubAbbr
来访问任何结构深度。通常,您希望使用structKeyExists(userAccountInfo, "personas")
或isDefined("userAccountInfo.personas")
来检查所有这些结构成员(尤其是数组)是否存在(不建议使用isDefined,因为它处理范围的方法不严格)。您也可以通过在每个支票structKeyExists(userAccountInfo.personas[1], "userClubList")
和structKeyExists(userAccountInfo.personas[1].userClubList[1], "clubAbbr")
上传递下一个成员来链接这些,依此类推。
无论如何,为了引导你更多,这是我对你的结构的理解:
<!--- test data (the one in your screenshot) --->
<cfset userAccountInfo = {}>
<cfset userAccountInfo.personas = []>
<cfset userAccountInfo.personas[1] = {}>
<cfset userAccountInfo.personas[1].personaId = "850074729">
<cfset userAccountInfo.personas[1].personaName = "IcedTube3">
<cfset userAccountInfo.personas[1].userClubList = []>
<cfset userAccountInfo.personas[1].userClubList[1] = {}>
<cfset userAccountInfo.personas[1].userClubList[1].clubAbbr = "Bel">
<cfset userAccountInfo.personas[1].userClubList[1].clubName = "Bell Ville FC">
<cfset userAccountInfo.personas[1].userClubList[1].established = "1363092161">
<cfset userAccountInfo.personas[1].userClubList[1].lastAccessTime = "1363092161">
<cfset userAccountInfo.personas[1].userClubList[1].platform = "360">
<cfset userAccountInfo.personas[1].userClubList[1].year = "2013">
<!--- let's make sure personas exists and is an array --->
<cfif structKeyExists(userAccountInfo, "personas") and isArray(userAccountInfo.personas)>
<cfset myPersonas = userAccountInfo.personas> <!--- myPersonas will be an array --->
<cfloop array="#myPersonas#" index="persona"> <!--- persona is supposed to be a struct --->
<!--- let's make sure each item in the array is really a struct --->
<cfif isStruct(persona)>
<cfoutput>Start reading persona.<br />====================<br /><br /></cfoutput>
<!--- read personaId --->
<cfif structKeyExists(persona, "personaId")>
<cfoutput>personaId: #persona.personaId#<br /></cfoutput>
<cfelse>
<cfoutput>personaId: [none]<br /></cfoutput>
</cfif>
<!--- read personaName --->
<cfif structKeyExists(persona, "personaName")>
<cfoutput>personaName: #persona.personaName#<br /></cfoutput>
<cfelse>
<cfoutput>personaName: [none]<br /></cfoutput>
</cfif>
<!--- read userClubList (let's make sure userClubList exists in persona and is an array) --->
<cfif structKeyExists(persona, "userClubList") and isArray(persona.userClubList) and not arrayIsEmpty(persona.userClubList)>
<cfset myUserClubList = persona.userClubList>
<cfloop array="#myUserClubList#" index="club"> <!--- club is supposed to be a struct --->
<!--- let's make sure each item in the array is really a struct --->
<cfif isStruct(club)>
<cfoutput><br />Start reading club.<br />--------------------<br /><br /></cfoutput>
<!--- read clubAbbr --->
<cfif structKeyExists(club, "clubAbbr")>
<cfoutput>clubAbbr: #club.clubAbbr#<br /></cfoutput>
<cfelse>
<cfoutput>clubAbbr: [none]<br /></cfoutput>
</cfif>
<!--- and so on... --->
<cfoutput><br />--------------------<br />Done reading club.<br /><br /></cfoutput>
</cfif>
</cfloop>
<cfelse>
<cfoutput>userClubList: [none]<br /></cfoutput>
</cfif>
<cfoutput><br />====================<br />Done reading persona.<br /><br /></cfoutput>
</cfif>
</cfloop>
</cfif>
结果......
Start reading persona.
====================
personaId: 850074729
personaName: IcedTube3
Start reading club.
--------------------
clubAbbr: Bel
--------------------
Done reading club.
====================
Done reading persona.
希望这会有所帮助。 :)
答案 1 :(得分:2)
真的,这完全取决于你的最终目标。查看您在上面的结构中显示的数据模型,您正在为单个用户帐户构建单个记录,该帐户可以具有多个可以属于多个userClubLists的角色。下面的代码显示了如何填充。
注意:此格式适用于CF8 +。
<cfscript>
userAccountInfo = {
userAccountInfo = {
personas = [
{
personaID = "850074729",
personaName = "IcedTube3",
userClubList = [
{
clubAbbr = "Bel",
clubName = "Bel Ville FC",
established = "1361354191",
lastAccessTime = "1363092161",
platform = "360",
year = "2013"
},
{
clubAbbr = "New",
clubName = "New Club",
established = "1362139200",
lastAccessTime = "1363092161",
platform = "720",
year = "2012"
},
{
clubAbbr = "Old",
clubName = "Old Club",
established = "1261255543",
lastAccessTime = "1267444800",
platform = "180",
year = "2005"
}
]
},
{
personaID = "499137600",
personaName = "Marty",
userClubList = [
{
clubAbbr = "Biff",
clubName = "BiffCo Casino",
established = "307588800",
lastAccessTime = "1445390400",
platform = "1955a",
year = "1985"
}
]
}
]
}
} ;
</cfscript>
<cfdump var="#userAccountInfo#" label="UserAccountInfo" />