Coldfusion:将结构作为String传递给url

Dan*_*iel 5 coldfusion serialization struct

有没有一种简单的方法将单级结构序列化为字符串以便在URL中使用?

例如:

?key1=val1&key2=val2
Run Code Online (Sandbox Code Playgroud)

BKK*_*BKK 19

<cfscript>
// create simple struct
x = { a=1, b=2, c=3 };
WriteDump(x);

// serialize in JSON format and encode for URL transport
y = URLEncodedFormat( SerializeJSON(x));
WriteOutput( 'url: <a href="#SCRIPT_NAME#?z=#y#">#SCRIPT_NAME#?#y#</a>');

// now receive the URL variable and dump it
if ( StructKeyExists( url, 'z' )) {
    writeOutput( '<h3>URL Data:</h3>' );
    writeDump( DeserializeJSON( URLDecode( z)));
}
</cfscript>
Run Code Online (Sandbox Code Playgroud)


Pau*_*aul 13

这看起来怎么样?

<cfset tmpStruct = {"firstItem" = "one", "secondItem" = "two"} />

<cfset myUrl = "http://domain.com/file.cfm?" />

<cfloop list="#structKeyList(tmpStruct)#" index="i" >
    <cfset myUrl = myUrl & i & "=" & tmpStruct[i] & "&" />
</cfloop>

<cfset myUrl = left(myUrl,len(myUrl)-1) />

<cfdump var="#myUrl#" />
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记你也可以使用列表函数,即`for(键入tmpStruct){myUrl = listAppend(myURL,key&"="&URLEncodedFormat(tmpStruct [key]),"&"); }.两者都有效,虽然json的方法吸引了我的懒惰程序员;) (4认同)