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

时间:2012-06-15 23:06:09

标签: serialization struct coldfusion

是否有一种简单的方法可以将单级结构序列化为字符串以便在网址中使用?

例如:

?key1=val1&key2=val2

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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#" />