4

我有以下结构,我想做的是为其中的不同键(personaID、personaName、userClubList)设置变量,然后为 userClubList 数组中的值设置变量,但不知道如何工作向下遍历结构,找出它有哪些键,然后它们的键值是什么。

CF 结构

有人可以帮帮我吗?

4

2 回答 2

2

实际上,您已经能够使用此结构,并且不需要任何映射到其他变量。通过使用点符号userAccountInfo.personas或方括号来访问结构userAccountInfo["personas"]。您可以通过链接访问来访问任何结构深度userAccountInfo.personas[1].userClubList[1].clubAbbrstructKeyExists(userAccountInfo, "personas")通常你会想通过使用or来检查所有这些结构成员的存在,尤其是数组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.

希望这可以帮助。:)

于 2013-03-12T23:57:36.953 回答
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" />
于 2013-03-13T01:27:16.450 回答