Posted By

laurenceosx on 12/03/12


Tagged


Versions (?)

Groovy UrlEncode and UrlDecode a map


 / Published in: Groovy
 

lpt

  1. def urlEncodeMap( aMap ) { // e.g. given map [x:1, y2] returns x=1&y=2
  2. def encode = { URLEncoder.encode( "$it".toString() ) }
  3. return aMap.collect { encode(it.key) + '=' + encode(it.value) }.join('&')
  4. }
  5.  
  6. def urlDecodeToMap( aUrlEncodedStr ) {
  7. def result = [:]
  8. def decode = { URLDecoder.decode(it) }
  9. def ampSplit = aUrlEncodedStr.tokenize('&')
  10. ampSplit.each {
  11. def eqSplit = it.tokenize('=')
  12. result[ decode(eqSplit[0]) ] = decode(eqSplit[1])
  13. }
  14. return result;
  15. }

Report this snippet  

You need to login to post a comment.