2012-06-15 10 views

risposta

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> 
+0

Immagino che l'unica cosa che posso dire della mia soluzione sia che funzioni con strutture ancora più complesse. Paul digita più veloce di me!: D – BKK

+0

Ho pensato anche a questa soluzione. È più robusta, come hai detto, può gestire strutture complesse, ma devo sempre deserializzare JSON il json su ogni pagina ricevente ho fatto qualcosa di molto simile in questo esempio ma tramite il modulo: http://stackoverflow.com/questions/10392604/coldfusion-serializejson-example/10392839#10392839 – Paul

+0

Ottima risposta, Ben! –

13

Come funziona questo look?

<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#" /> 
+1

piuttosto bella in realtà :) – Daniel

+4

Non dimenticare è possibile utilizzare anche le funzioni di lista Intendi dire: 'for (chiave in tmpStruct) { \t myUrl = listAppend (myURL, chiave e "=" & URLEncodedFormat (tmpStruct [chiave]), "&"); \t}. Entrambi funzionano, anche se l'approccio JSON fa appello al programmatore pigro in me;) – Leigh

Problemi correlati