Search code examples
web-servicescoldfusioncoldfusion-8

Convert name value pairs to struct


It's been awhile since I've written this type of ColdFusion code, hence the question.

I am returning values from a .NET webservice into ColdFusion. The webservice returns an array of strings. The following code...

<cfoutput>
 <cfset xArrayItems=#GetRequestedUserSettings.settingValues.getString()#>
 <cfset xLen=ArrayLen(GetRequestedUserSettings.settingValues.getString())>
 <cfloop index="x" from=1 to="#xLen#">
    #xArrayItems[x]#<br />
 </cfloop>
</cfoutput>

results in the following output ...

maxsize=50
isdomainadmin=False
seenwelcome=False

I want to put those name/value pairs into a meaningful structure so that I can reference them farther down in the code. I actually need to pass them in as a cfinvokearguments for the next webservice call.

Could someone please be kind enough to remind me how to do this in CF8? Most of what I am finding refers to newer versions.


Solution

  • I ended up with something quite similar to what @Henry you provided.

    <cfset UserSettings = structNew()>
        <cfset xArrayItems= GetRequestedUserSettings.settingValues.getString()>
        <cfset xLen=ArrayLen(GetRequestedUserSettings.settingValues.getString())>
        <cfloop index="x" from=1 to="#xLen#">
            <cfset varName = ListGetAt(xArrayItems[x], 1, "=")>
            <cfset varValue = ListGetAt(xArrayItems[x], 2, "=")>
            <cfset "UserSettings.#varname#" = varValue>
        </cfloop>
    

    Not sure if an Array or a Struct is a better solution, but the both work in the end.